I am working on delivering PDF files that are generated on demand to users of mobile devices. I am using ASP.NET MVC 2 and I generate the PDF using the Report Viewer local report export as PDF.
This gives me the raw byte[] of PDF content.
I then return it using:
return File( pdf , mimeType , "FileName.pdf" );
Note, I have tried it with and without the "FileName.pdf" default name for saving the file.
It works great in desktop browsers and iPhones. I have tested with the WP 7 emulator and it seems like it would work if it could just read PDFs (it prompts for the proper file name to save it).
Android seems to be complaining, however.
I have tested on Droid-X and Droid devices and it always shows " Download unsuccessful" in the browser download results. On Opera Mini it prompted to save the file (when using the return method that included the file name) and the PDF opened fine after it was saved on the device.
I can return PDF files that already exist on disk just fine with: File( "~/filename.pdf" , "application/pdf" ); so it seems to just be a problem with dynamic PDFs.
Has anyone dealt with this issue before?
I have tested on Droid-X and Droid devices and it always shows " Download unsuccessful" in the browser download results.
AFAIK, that means either that the HTTP request failed or that the MIME type was not one for which there is an ACTION_VIEW activity registered. Bear in mind that the DROID did not ship with a PDF viewer, and I forget if the DROID X did or not.
I agree with Chris, in that it feels like the problem is an invalid MIME type. Consider using curl or a browser plugin to inspect your HTTP response headers and confirm that, indeed, application/pdf is being returned as the MIME type.
If that does not help, and you can supply us with a URL to test, that would help as well.
I see this questions id from years ago, but if you just want to display the pdf, you can embed your pdf URL in google doc viewer, and then reference that URL, then it should open on android browsers.
However, this will only work if your URL is publicly accessible.
Related
I have to download some files from a server which has 2 stage authentication for a PowerPoint Addin I am trying to develop.
First, I log into the workspace through a browser...
In that browser I can call a .txt file and the contents are displayed in the browser - Great!
In my PowerPoint Addin I then have the following code for download a PP file and open it...
Globals.ThisAddIn.Application.Presentations.Open(#"https://workspace2.blahblah.com/group/corenarratives/Shared%20Documents/corenarratives/BlankPresentationTemplate.pptx");
This downloads the pptx file and opens it perfect! - Great!
I then try and download a .txt file with this code:
WebClient wc = new WebClient();
wc.DownloadFile("https://workspace2.blahblah.com/group/corenarratives/Shared%20Documents/corenarratives/rts.txt", #"C:\trev\trev.txt");
And the contents of the file contains a html error page...
When I save the .txt file as a .html file and open it in a browser it redirects me to the workspace login page...
I don't understand why the PowerPoint file opens and the .txt file doesn't?
And, how, if possible, to download the .txt file?
Can anyone help please?
Thanks
Is it ok to post an answer I would describe as "in progress"?
I know I guy on twitter who really knows his stuff.
Not a close friend but someone who I had followed for a long time. he wrote fiddler.
So, I stuck my neck out and asked him.
This is what he said.
"Watch your traffic from each scenario with Fiddler. Is PPT sending a Cookie, Auth header, or User-Agent your code needs to send?"
"WebClient isn't based on WinINET/URLMon. PowerPoint downloads (often) are, and that means it gets cookies, UA string, etc."
"PowerPoint has cookies and automatic authentication behaviors inherited from URLMon/WinINET."
Which, if I understand correctly, explains why PowerPoint can download a file..
I think.
Update:
I ended up implement this:
Is it possible to transfer authentication from Webbrowser to WebRequest
HTH
Hi guys this is my follow up question and I think this is the real issue here. Click Here
Whenever I access the file using the directory browsing (eg. http://localhost/temp/1.pdf) it always render the old or previous PDF file even if I change the entire file with the same file name, example I have 1.pdf with some content then I delete this file on the directory then replace with new and different content and rename it to 1.pdf then when I access it using browser it always render the previous value not the new one that I replace. This only happens on IE and Opera browser. Please need help this is a production issue.
Edit:
I found something very strange, example i have this url for the location of the pdf file (eg. http://localhost/website/tempfolder/1.pdf) the first pdf that I generated will show then some part in my code I change the casing of some letter example 't', I change it to 'T' then the newly pdf shows but when I revert back to the old case expecting that it will show the new pdf but sad to say the first PDF file shows.
Your browser is probably caching the PDF files.
For IE, you can do a CTRL F5 to force it to reload from web server.
Or put a random query string in your URL
e.g. /temp/1.pdf?v=1, /temp/1.pdf?v=2
Assuming you server is IIS, permanent solution is to configure HTTP response headers.
Go to IIS manager
Navigate to your folder
Click "HTTP Response Headers" in Features View
Right-button click/select "Set Common Headers"
Check "Expire Web Content" and select "Immediately".
I have a question about outputting PDF files. Currently I'm storing the PDF in the database in binary form. I'm outputting the PDF file via a URL such as:
http://myhost.com/FileManager.aspx?FileId=8465b2f9-b64e-4a9a-a449-94b5adb3b278
so from what I could deduce, to the browser this is an .aspx page that is loading and not a PDF. Firefox and IE interpret this correctly and launch Adobe Acrobat, however since Chrome (12.0.742.112) has its own implementation of a PDF reader, it will open the file correctly, but then when someone goes to save the file in chrome, it wants to save it as a .aspx. If I simply rename the .aspx to .pdf, the file downloads correctly. However, I"m trying to avoid telling my customer that s/he may have to take that extra step.
When I chose to look at the headers that loaded in Chrome via Web Inspector, I see this:
FileManager.aspx:-1 Resource interpreted as Document but transferred
with MIME type application/pdf
I can completely understand why Chrome would say this.
Furthermore, I get a save as box upon page load when I add:
Response.AddHeader("content-disposition", "attachment;filename=blah.pdf");
However, I was hoping to just keep the file in a browser. So aside from using some URL Rewrite, is there a way I can manipulate the HTTP Headers to simply open the page as a PDF and save correctly in Chrome?
Lastly, I tried using a WebService, but I can't seem to write the binary data to the page.
this.Context.Response.BinaryWrite(bytes);
Any help is appreciated!
In the web application's top-level web.config, add the following <add> element to the <httpHandlers> section:
<httpHandlers>
<add verb="*" path="FileManager.pdf" type="ProjectName.FileManager" />
...where ProjectName.FileManager is the full name (namespace and classname) of the FileManager class in FileManager.aspx.cs.
This tells ASP.NET to handle the path /FileManager.pdf using the handler defined by ProjectName.FileManager. (System.Web.UI.Page implements the IHttpHandler interface, so every webform is a handler.)
Now you can serve the PDFs via a URL with a .pdf extension like so:
http://myhost.com/FileManager.pdf?FileId=8465b2f9-b64e-4a9a-a449-94b5adb3b278
You do not need to rename the physical FileManager.aspx file. When the user enters /FileManager.pdf?FileId=foo in the browser, ASP.NET will handle the request with the ProjectName.FileManager class defined in FileManager.aspx.
Note:
The above should work on Cassini (the Visual Studio "mini" webserver) with no further changes. However IIS by default only sends *.aspx, *.asmx, *.ashx requests to ASP.NET. Therefore, for the above to work on IIS, you need to tell it to send requests for /FileManager.pdf to ASP.NET. That is, you need to configure a "mapping".
With IIS 6 you need to configure the mapping using IIS manager.
With IIS 7 you can configure a mapping from your web.config--this makes deployment easier, but it depends on how your hosting is set up.
Something that worked for me was to add the intended filename between the application page name (in your case, FileManager.aspx) and the query mark (the ? mark).
So, in the end, you'd have something like this:
http://myhost.com/FileManager.aspx/myfile.pdf?FileId=8465b2f9-b64e-4a9a-a449-94b5adb3b278
Surprisingly, the correct application will run (Filemanager.aspx), the variables will be passed unharmed (FileId), the result will be treated as inline (if you didn't change it to attachment, that is) but the browser will think that the filename to use, in case the user wants to save the page, is myfile.pdf.
I am looking for a means to generate a iCalendar feed using .Net , atm I am using the DDay library to generate the iCal file and write it to the response.
The problem with this is if I feed the URL to outlook it says that it is a invalid ics file.
I have tried various routes including :
Using MVC routing so that the link contains a .ics extension.
Changing the Mime Type so that it is Text/Calendar.
One observation I have found is that if I navigate to the URL in a browser a valid iCal file is generated and is imported into Outlook correctly. Secondly if I take that exactly file and host it via IIS as static content , Outlook picks it up as a valid file.
The problem is that I need to generate a Dynamic feed and seem to have run out of things to try and get this working.
It seems the problem in my case was that the default asp.net auth was blocking outlook because it was not authenticating , once a exception was added to the web.config , the URL loaded correctly in outlook
Hii,
My requirment is to show a dynamically created pdf file directly to my web page. It works fine for the system which is having pdf reader software. But for the system which does not have the pdf software it is showing error like below
The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.
An invalid character was found in text content. Error processing resource 'http://localhost:4252/OmanePost/Customer/EBox/PD...
I need to handle this situation bit differently.i.e In this situation the file should be save to the physical location of the system for that i need to identify whether the client machine has pdf software or not then i can manage properly
I m using ASP.NET 2.0 version
It looks to me that you are serving your PDF with an XML mime/content-type. Make sure you set your content-type to application/pdf and you'll probably get a more suitable browser response.
In this case the browser should ask the user to open the file in an external application.
Please verify that you are sending the correct Content-Type: application/pdf header. Certain versions of Microsoft's browser ignore the content-type header, so you need to specify a filename ending in .pdf in the content disposition header: Content-Disposition: inline; filename=filename.pdf;
Note: I have not verified that it works with "inline" instead of "attachment", but I think it is worth a try.
My requirment is to show a dynamically created pdf file directly to my web page.
Try online ZohoViewer that takes a PDF file link and displays in the browser without requiring PDF reader on the client machine. As such there's no way to check if the client machine has a pdf reader or not.
You can not identify that client system has pdf software using javascript, asp.net, c#.
If the PDF reader software is not there and the PDF is a valid PDF then it should not throw exception. Instead it asks for a software in client machine which can read the file.