I am using iTextSharp for creating PDF for reports in ASP.Net (c#).
Pdf is created successfully & can be opened when running the Application, but when iam hosted in IIS it cannot open in PDF and gives error :
"adobe reader could not open pdf because it is either not a supported file type or because the file has been damaged "
My code like:
Document pdfDoc = new Document();
pdfDoc.SetPageSize(PageSize.A4.Rotate());
string filepath = "Demo" + ".pdf";
string attachment = "attachment; filename=" + filepath;
Response.Charset = String.Empty;
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/PDF";
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
//Adding some paragraph in to the pdfdoc.
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
I am waiting for your greatfull ideas
I know that this an old post, but I had a problem with itextsharp, and in my case was about the folder permissions to the iis user.
I just right clicked on my main folder, went to the security tab and chose the .Net User(were 2 for me) and gave full control. And that was my problem, and solved. Hope this can help someone else.
Related
In Dynamics CRM online, I have a record in the annotation entity which has an XML attached.
From a plugin, I need to get the content of that attachment and create an XML element so I can deserialize its content as an object class and apply some logic.
As we are in an online implementation, I cannot do things like create a file and read its content.
So what we have is a document content in the attached file, which is a string with multiple characters, as you can see here (attachment is generated by a Power Automate flow):
Here is some of my code:
byte[] fileContentBytes = Encoding.Default.GetBytes(note.DocumentBody);
MemoryStream stream = new MemoryStream(fileContentBytes);
StreamReader globalReader = new StreamReader(stream);
XmlDocument doc = new XmlDocument();
doc.Load(globalReader);
The error message says that Data at the root level is invalid. Line 1, position 1, as it's trying to parse as an XML something that is not parse-able as that.
Power Automate is generating JSON document, you are trying to parse it as XML, so failing.
Instead try some methods to convert to your needed format.
The document body is base 64 encoded.
Try the following
byte[] fileContentBytes = Convert.FromBase64String(note.DocumentBody);
MemoryStream stream = new MemoryStream(fileContentBytes);
StreamReader globalReader = new StreamReader(stream);
XmlDocument doc = new XmlDocument();
doc.Load(globalReader);
I am trying to open a PDF file that represents the documentation of my plugin from inside a package, since I have opened a properties file the same way using getClass().getResource(URI).
I am trying the same with the PDF file, and I'm trying to get the URL of the file, then passing it to Desktop.browse() converted to URI, but it gives me a Malformed URI exception. Is there a way to do this easier and also to work?
This is my code so far:
try{
URL url = new URL(getClass().getResource("Documentation.pdf"), null);
Desktop.getDesktop().browse(url.toURI());
}catch(Exception exception){
Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, exception.getLocalizedMessage(), exception);
ErrorDialog.openError(null, "Error", "Error occured!", status);
}
The URL/URI you get back from getResource uses the bundleresource scheme which is not understood by many things.
For an Eclipse plugin you should use the FileLocator class.
Bundle bundle = FrameworkUtil.getBundle(getClass());
IPath path = new Path("path relative to root of the plugin");
URL url = FileLocator.find(bundle, path, null);
URL fileURL = FileLocator.toFileURL(url);
Desktop.getDesktop().browse(fileURL.toURI());
Again the URL returned by FileLocator.find uses a special scheme and is not understood by many things. FileLocator.toFileURL converts this URL to a normal file scheme - to do this it may be necessary to unpack the file from the plugin jar to a temporary location.
Note: Path is org.eclipse.core.runtime.Path
How do I download files from my gridFS store in Mongodb?
I have the following code:
#bottle.route('/download')
def download():
file_id = ObjectId(bottle.request.query.id)
if file_id:
try:
file_to_download = fs.get(file_id)
except:
return "document id not found for id:" + file_id, sys.exc_info()[0]
return (file_to_download)
return bottle.template('files_test/download')
which returns gibberish in the browser. How do I specify that I want it to be downloaded?
Edit:
This is the hyperlink I want to use to download the file:
<--Download File-->
Have I missed it here well?
See this question:
flask return image created from database
You need to set the Content-Type and Content-Disposition headers so the browser knows what type of file you're sending, and that it should be downloaded rather than displayed.
i mode development in eclipse. the fileupload works just fine. but i will make directory to /var/wms/year/month/file.jpg on linux. this my source code from client:
add component to form
fileUpload = new SingleUploader(FileInputType.LABEL);
fileUpload.setFileInputPrefix("PJ");
fileUpload.addOnFinishUploadHandler(onFinishUploaderHandler);
layoutContainerItemRight.add(fileUpload, formData);
method is addOnFinishUploadHandler
private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
public void onFinish(IUploader uploader) {
if (uploader.getStatus() == gwtupload.client.IUploadStatus.Status.SUBMITING) {
String month = VisionProperties.getBulan();
String year = DateTimeFormat.getFormat( "d-M-yyyy" ).format( new Date() ).split( "-")[2];
String strDirectoy = "/var/wms/" + year + "/" + month + "/";
File file = new File(strDirectoy);
if (!file.exists()) {
file.mkdirs();
}
}
if (uploader.getStatus() == gwtupload.client.IUploadStatus.Status.SUCCESS) {
String msg = uploader.getServerInfo().message;
fileName = msg.toString();
if(selectWindow != 2){
img.setUrl("servlet.gupld?show=&fieldname=" + fileName);
itemPanel.render(img.getElement());
}else{
tb.setVisible(true);
tb.setText("Download File "+uploader.getFileName());
}
}
}
};
how to make directory file when upload file process?
You are trying to use to use java.io.File in the client side which is not supported by the set of packages in the GWT jre emulation.
If you want to do this in client side you have to use the javascript File Api which is not supported by old browsers, and is not implemented in gwt-core. Using elemental you could use the Api only with Chrome, but I'm not positive. So it is better to wrap it via jsni, it is planned in gwtupload, but there is no a timeframe yet. Be aware that using js File Api, you dont have access to your real filesystem, but a virtual one inside your browser. To save created files in the local filesystem you have to download it using and iframe so as it asks the user where to save it.
Otherwise, If you wanted to do this work at server side, do it overriding the executeAction in your servlet if you are extending UploadAction.
You cannot do this on client side. You can perform this on server side in the following ways
before you upload the files to server by another rpc/http call.
after you upload the files to server when the file upload servlet is being executed on the srever side.
HTML5 FILE API are restricted to readonly behavior in even modern browser.
Reference -
1. Basic File upload in GWT
2. How to retrieve file from GWT FileUpload component?
I'm using Crystal Reports and have some code that returns a stream of a PDF report. When The report is done being generated, the browser (IE8) prompts for the user to open or save the report. It works but behind the scenes an exception is being thrown. We handle the exception but the handling causes some other problems.
// This completes but throws an exception.
rpt.ExportToHttpResponse(crExportOptions.ExportFormatType, resp, true, fileName);
The solution according to a post on the SAP site works and the exception is no longer thrown. However, the problem is we no longer have the option to open or save the file. The report is just opened in the browser.
System.IO.Stream oStream = null;
byte[] byteArray = null;
oStream = report.ExportToStream(crExportOptions.ExportFormatType);
byteArray = new byte[oStream.Length];
oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length - 1));
response.ClearContent();
response.ClearHeaders();
response.ContentType = "application/pdf";
response.BinaryWrite(byteArray);
response.Flush();
response.Close();
report.Close();
report.Dispose();
I want my cake and I want to eat it! I don't want the exception thrown (solved by the SAP solution) but still want the option to open or save the PDF returned. How do I modify the above code the present the user with the option to save / open?
Thanks!
Found a solution in this SO post.
added this to my code...
response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", reportName));