I am working on an asp.net/c# web application that allows users to view and download PDF files. When I am clicking on a file, I get to view that in the PDF reader available in the browser, and when I save it, the file should be saved with the name that I have passed via headers. But this is not the behavior in IE7 & IE8
FileInfo f = new FileInfo(FileName);
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline; filename=" + f.Name)
When I click to save a file, the filename that I am sending over is not being used to save the file, but the filename of the aspx page in the url is being taken. Is this is a bug in IE. Everything works fine when I try it on Google chrome.
Try Response.AddHeader("Content-Disposition", "attachment;filename=\"" + f.name + "\""); Inline is not working under IE 7-8.
Related
My code is opening a PDF in a tab/new window using an .apsx page and the following code:
string fileName = GetFileName(so);
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline; filename=" + fileName);
Response.BinaryWrite(pdfStream);
When selecting the save option in the browser for the PDF, the filename in the save dialogue is not being populated correctly in IE (11); it's still using the page name. Chrome and FF are working like I'd expect them too (using the provided filename as the suggested name).
In addition, IE works the same as Chrome and FF when i change the content-disposition to attachment instead of inline. They all use the filename as the suggested save name.
Am I missing something in how to use the inline functionality?
Did you try change inline to attachment ?
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
I have created a PDF using iText and storing it in a particular location (specified in the code). I would like to prompt a save dialog box for the user to choose location on his computer to save the pdf. I checked iText tutorial but it didn't help me.
Here is the code for generating the PDF file:
Document objDoc = new Document();
PdfWriter.GetInstance(objDoc, new FileStream("C:\\HelloWorld.pdf", FileMode.Create));
objDoc.Open();
objDoc.Add(new Paragraph("welcome iText Pdf"));
objDoc.Close();
I tried like this for saving:
string FileName ="HelloWorld.pdf";
String FilePath = #"C:\";
HttpResponse response = HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath + FileName);
response.Flush();
response.End();
I'm assuming you're doing this from a web page since you tagged this ASP.NET. You need to add the Content-Disposition header. See the following question for details:
Force download of a file on web server - ASP .NET C#
It shouldn't be this hard to find out how to do this. Basically I'm trying to take a string and let the client save it when they click a button. It should pop up with a Save/Open dialog. No extra bells and whistles or anything. It's not rocket science, (or so I would've thought).
There seems to be a ton of different ways, (StreamWriter, HttpResponse, etc.), but none of the examples I've been able to find work properly or explain what's going on. Thanks in advance.
An example one of the many blocks of code I've found...
(This is just an example, feel free to not base your answer around this.)
String FileName = "FileName.txt";
String FilePath = "C:/...."; //Replace this
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath);
response.Flush();
response.End();
Line 2 says to replace that string. How? This code was advertised as bringing up a dialog. I shouldn't be having to set a path in the code, right?
EDIT: Final Outcome (Edited again, Delete has to come before End();)
string FilePath = Server.MapPath("~/Temp/");
string FileName = "test.txt";
// Creates the file on server
File.WriteAllText(FilePath + FileName, "hello");
// Prompts user to save file
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath + FileName);
response.Flush();
// Deletes the file on server
File.Delete(FilePath + FileName);
response.End();
Line 2 (FilePath) indicates the path to the file on the server
Line 8:
response.TransmitFile(FilePath);
Transmits that specific file to the client and THAT is what pops the save dialog.
If you don't transmit the file, I'm not sure if the dialog will pop up at all (even though you set a header)
Anyways, I think line 8 should read:
response.TransmitFile(FilePath + FileName);
There will be a default dialog box by browser, if it will find Response as some file. If you want browser to display that default dialog box, all you need to do is send response to browser as file, which you can do in number of ways:
If it is a static file,
best way is to just mention path of file in anchor tag's href.(obviously if you don't have security concern)
Just out along with your response, the way it is done in your example.
Other ways you can refer here 4 ways to send pdf from asp.net
If it is a dynamic file which you need to generate at run time, you can do a trick, generate the file from filestream, put it in some temporary folder at server, read it back as a static file as mentioned above.
Just use this code it should work to prompt the user to open a dialog for opening or saving the file on the system ....
byte[] bytesPDF = System.IO.File.ReadAllBytes(#"C:\sample.pdf");
if (bytesPDF != null)
{
Response.AddHeader("content-disposition", "attachment;filename= DownloadSample.pdf");
Response.ContentType = "application/octectstream";
Response.BinaryWrite(bytesPDF);
Response.End();
}
FilePath is supposed to point to the file you want to send to the client. This is the path on the server.
I have to give one option in my website to upload multiple files and then allowing users to download those files.
I have done uploading multiple files part, however I am not much clear how I will do downloading part.
I first thought to add hyperlinks dynamically to a label for each file (as I am not sure how many files user will upload).
But then it opens file in browser and does not give option to save or open file.
Main issue is user can submit any type of file like ms doc or xls or text files etc Hence content type is not fixed.
I am not clear on how exactly I will do it I mean adding link buttons dynamically or adding hyperlinks dynamically. And after that how will I download file? I am not able to do
Response.WriteFile(Server.MapPath(#"~/logo_large.gif"));
as content type is not clear.
Please help me regarding code for download all types of files
for download the file:
public void DownLoad(string FName)
{
string path = FName;
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
else
{
Response.Write("This file does not exist.");
}
}
but this is for word doc/docx files. in the line: Response.ContentType = "application/octet-stream"; you have to define the type of the file.
for displaying hyperlink dynamically write a loop and go over all the files which were uploaded by the user and writh the following code:
yourdivId += "<a href='" + file.FullName + "' >" + file.Name + "</a></br>";
Hope this will help u.()
Response.Clear();
Response.ContentType = YourFile.ToString();
Response.AddHeader("Content-Disposition", "attachment;filename=" + YourFileName);
Response.OutputStream.Write(YourFile, 0, YourFile.Length);
Response.Flush();
Response.End();
I have tried this to download img,txtfile,zip file,doc it works fine..
but little problem user will face that in my case when i opened a word(doc) file its ask
me to openwith..? when i select MsWord it opened it correctly.
For showing these file you can make a grid view to show all file with a download link...
Actually you can follow different ways.
Guide the user during upload and make him specify the file type before starting upload. So the user has to select the content type from a selection. You need to save the correlation between the file and its content type.
Create a map between file extensions and mime-types (See here for example). You need to obtain the file extension and save the correlation between that file and its content type.
Try to identify the content type automatically. There is a Windows
API that let you identify the Mime Type of a file. And here is some code.
After that, you can use st mnmn solution.
You can do it like this:
try
{
string strURL=txtFileName.Text;
WebClient req=new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer= true;
response.AddHeader("Content-Disposition","attachment;filename=\"" + Server.MapPath(strURL) + "\"");
byte[] data=req.DownloadData(Server.MapPath(strURL));
response.BinaryWrite(data);
response.End();
}
catch(Exception ex)
{
}
You'll have to set few response headers before server sends the file to client. See How to Download a file with a hyperlink
How can I force the Save As Dialog box to show within an asp.net page without getting the browser bar at the top saying "To help protect security IE blocked this site from downloading files..... Click here for options"
It then forces the user to click the Download File option - but this first time they do this nothing happens.
What I need to happen is that the File Download box should be displayed with the options of saving or opening etc.. the file to download.
The asp.net/C# code I am currently using is:
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=myfilename");
Response.WriteFile(myfilename);
Response.End();
Any ideas why I shouldn't just get the File Download box ?
Try to change the Content Type to "Application/Force-Download" instead
ASP.NET C# Example:
Response.AppendHeader("Content-Type", "application/force-download;");
Fully functional code:
try
{
string FileName = Request.QueryString["file"];
Response.AppendHeader("Content-Type", "application/force-download;");
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.WriteFile(#Server.MapPath("download/") + "\\" + FileName);
Response.End();
}
catch (Exception)
{
//Handle Error
}