We have a requirement where I need to open the pdf file from particular location in server i.e. "C:\PdfFile\Test.pdf".
I have tried this solution:
string fileName = lnk.CommandArgument.ToString();
System.Diagnostics.ProcessStartInfo a = new System.Diagnostics.ProcessStartInfo(fileName, "Open");
System.Diagnostics.Process.Start(a);
This is working for local as we have same path in our local But this is not working when we host the site.
In you ASP.NET form application you have to add this code:
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=MyFile.pdf");
Response.TransmitFile(Server.MapPath(#"C:\PdfFile\Test.pdf"));
Response.End();
Related
I'm trying to use below code, to allow the user to download an excel that is located on a local server:
FileInfo dest_file= new FileInfo(filename);
Response.ClearContent();
Response.ContentType = "Excel/xls";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}", filename));
Response.AddHeader("Content-Length", dest_file.Length.toString());
Response.TransmitFile(filename);
Response.End();
However, after publishing the solution, downloading the file results in a Network failure. Any idea how I can solve this?
Note that the file downloads successfully when trying to access it through directory browsing.
it seems your using an invalid content-type
for xlsx use this
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
for xls
Response.ContentType = "application/vnd.ms-excel"
I want to download an Excel sheet to my specific location not to Download folder.
I tried below code. Then the file is download to the "Download" folder. I need to download this file to a given file path.
string filepath = "D:\";
string strDownloadableFilename = "Test.xlsx"
Using (MemoryStream stream = ExportExcel()){
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", strDownloadableFilename));
stream.WriteTo(Response.OutPutStream); ;
Response.End();
}
The file is download to "Download" folder.
Well there are two things
first of all what you want to achieve can't be done by downoad option it means we can read file and write in system memory as per our desired location but in that case No download popup will see so you have to decide if you need download popup and no desired location or vice versa.
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.
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#
I want to download a file from another server with save as dialog box... I tried 1 example to download file from our server which worked but i don't know how to work for files which are hosted on another server.
If I try to download then i am getting It is not valid virtual path
Response.WriteFile(Convert.ToString(http://abc.com/sbe/test.pdf));
What should i do to download files from another server.
Regards,
I got this code from a guy in asp.net forums.. His code help me to resolve my issue for a complete discussion check this link out : http://forums.asp.net/p/1772874/4847084.aspx/1?p=True&t=634655765939994111
Below is the code
WebClient client = new WebClient();
string url = #"http://www.agiledeveloper.com/articles/BackgroundWorker.pdf";
byte[] data = client.DownloadData(new Uri(url));
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}", "aspnet.pdf"));
Response.OutputStream.Write(data, 0, data.Length);