I am storing some files like PDF, Doc, Docx, xls etc. in the file system.
Now I have to show those files on the browser for the users to view.
So basically a link button is there in a datagrid, upon clicking on which the user will be able to view.
So far my code goes like this
Response.Clear();
// Response.ContentType = "application/pdf";
//Response.ContentType = "application/x-msexcel";
//Response.ContentType = "application/msword";
string strFilePath = #"C:\test.pdf";
// string strFilePath = #"C:\test.doc";
Response.WriteFile(strFilePath);
Response.End();
This works fine for pdf files but fails for word or excel files. In the browser I am able to view the pdf but the other files are not opening in browser but are asking for the
Save , Open dialog box option. If I click on Open, it will open in a normal window instead of browser.
How can I achieve my target? The browser is IE
Thanks in advance
have you tried adding something like this?
Response.AppendHeader("Content-Disposition", "inline;filename=" + "ExcelFile.xls");
There may be a browser configuration issue.
Try changing some settings in the browser(Custom Level).
Launching programs and files in an Iframe
Launching applications and unsafe files.
etc
Related
When I upload a docx, xlsx or pptx to amazon S3 using aws .net sdk the file is getting uploaded fine and I am able to view the file directly from S3 without any issues. But when I download the file in ASP.net using C# I am getting a warning message (see below) when opening the file:
"Excel found unreadable content in test.xlsx. Do you want to recover the contents of this workbook?" If I click yes, I am able to see all the contents in the document.
similarly I am getting a warning message for .docx file as well.
This is the C# code I am using:
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.OutputStream.Write(Content, 0, Content.Length);
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
Update: When I download the file in windows forms app it is working fine and the problem is with web app only.
What am I doing wrong?
Please help...
More information is needed to figure this out. The code snippet you provided looks OK. The problem looks to be elsewhere in your code... For example, what are you loading inside "Content"?
Also, it would help if you tried uploading and downloading a simple text file, to see what kind of data gets appended.
If Content is a byte[] created from a MemoryStream, make sure you're calling .ToArray() and not .GetBuffer() to get it. If you use .GetBuffer(), you'll potentially get extra bytes on the end, which the office file format probably doesn't like. Your other file types you've tried it with might be more tolerant of extra data they weren't expecting on the end.
I have some images in my Asp.net web page. My requirement is to save a selected image to users disk while clicking a save button.Normally all browser has the provision for right click save as option.But I have to implement this feature on an html input button click. I have the following questions.
How can I make a script to do this functionality which should work on all major browsers
Do we get any jQuery library which does this functionality
Can I get any alternative in asp.net technology rather than java script/jQuery for doing the same functioanlity
Can anyone please help me?
jQuery is a client-side javascript library. It is not involved with serving files to download.
You can provide a download on any button click in asp.net. jQuery is not required for downloading files.
Your code in the event handler would look as follows:
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", fileName));
Response.WriteFile(filePath + fileName);
Response.Flush();
Response.End();
You cannot write a file straight to disk from a website, period. That's just sensible security. A regular download link/button will always invoke the standard download process of the browser, in which you have no say whether the file will be saved at all or where to. The only way to circumvent that is to create a native browser plugin which the user will have to install first, which will write files from your site straight to a specific location on disk. Please be extremly careful writing such a plugin should you do so so it does not become a gaping security hole for everybody.
Hi there. I think this one is a tough one, but I hope someone can help.
I have a folder which is downloaded from my website. The folder contains a Main PDF report on a housing property, and another folder containing pictures and more PDF reports on similar houses in the area.
On my main PDF report I would like to have a link to open the other PDF folders.
How would I go about creating a link to open a PDF report saved in a file?
Looking at the picture below, where it says CS1.pdf, that is the name of the PDF and where the link will be. This is a screen shot of a portion of my main PDF.
I don't mind if the PDF reports are opened in a webpage, in fact I would prefer it. But, at the moment, I just need a simple link created that will open a PDF located in a folder.
An explanation of the app flow:
Here below I save the PDF that I want to link to later. It's saved to a temp location; the exact location is not important as it will be deleted after the file uploads.
The file I upload contains a few pictures, some PDFs and a data.XML file. When a person downloads the file, the data.XML file is used to create my main PDF, or report, in any format. I need to send a link through the XML file that will appear on the report making it able to link to the other PDF files that were uploaded with the data.xml file.
CS1[12].TagValue = ReportDS.Tables[9].Rows[0].ItemArray[0].ToString();
//Save PDF
//PDFName = System.Guid.NewGuid().ToString() + ".pdf";
string PDFName = "CS1" + ".pdf";
WebClient webClient = new WebClient();
webClient.DownloadFile(CS1[12].TagValue, "C://Users//Shaun//Documents//FormValue//" + PDFName);
CS1[12].TagValue = PDFName; //This is the value passed to the main PDF so this is where i should pass the link through.
The line CS1[12].TagValue = PDFName; is where I set the value to send though to the main PDF, and this is where my link should be sent through.
I need something like CS1[12].TagValue = "http://C://Users//Shaun//Downloads//CS1.pdf"; but with the file path of the downloaded location.
you are on a right way but you have some mistake like, when you giving location of pdf file from downloaded location, that must be a file type, its not from a server...
CS1[12].TagValue = "http://C://Users//Shaun//Downloads//CS1.pdf";
but it must like this
CS1[12].TagValue = "file://C://Users//Shaun//Downloads//CS1.pdf";
so that you have change one line in your code...
webClient.DownloadFile(CS1[12].TagValue, "file://C:/Users//Shaun//Documents//FormValue//" + PDFName);
I've created a word document generator in C sharp and I want that generated word document to be save in a specific place in the client. I'm looking for a similar functionality like FolderBrowserDialog in Windows Form. I'm using ASP.Net and I tried may solutions but still no luck. Anyone can help me.
No! server (web-app) program don't have an ability to save a generated document at specific place at client.
try to use the HttpResponse in behind's code:
like:
// Clear the content of the response
Response.ClearContent();
// Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + savedNameWithExtension);
// Add the file size into the response header
Response.AddHeader("Content-Length", myfile.Length.ToString());
// Set the ContentType
Response.ContentType = ReturnExtension(myfile.Extension.ToLower());
// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(myfile.FullName);
// End the response
Response.End();
This section of code will prompt the user to save the specified file on a certain location on his machine.
Hope this is clear.
When you download a file from the browser its the browser to show the save file dialog and this works on all browsers and on all platforms. Just keep the default behavior and let users decide location and file name. I guess any hacky implementation of that part if possible at all, it is likely to break in some browsers or platforms, like mac, ipad, android...
you do not need to specify any control to be used, once you call the methods to download a file like
Response.WriteFile or Response.BinaryWrite or any other, the browser takes care of everything else for you and let it be like that ;-)
I am dynamically generating html file. It is formated with Css and Images. I am looking for code which should able to execute on button click, and ask the location for saving file. also it should download concern resource files which has referenced in html file. What code i have to do ? Can i use WebClient for the same ?
There is no "Save As" dialog in ASP.NET. Since it is running in a browser. You have no access to the user's file system, including the Save As dialog.
But you can send the user a file, as an attachment, most browsers will display a dialog asking the user whether to save the file or open it. Maybe the user will choose to save it.
Response.ContentType = "application/octet-stream" (or content type of your file).
Response.AppendHeader("content-disposition", "attachment;filename=" & strFileName)
You could create a situation where you can use WebClient if you make your file available through a uri, see this post.
Yes, you can use WebClient. Option is to use HttpRequest and HttpResponse.