I am making a web site in asp.net with c sharp.
I need to place a download file functionality(pdf, doc, xls) on one of my web page.
How can I do that?
If you want to do this automatically when a link is clicked from the server side, you have to send the file back yourself rather and add a couple of custom headers to the output. The way to do this is to use Response.TransmitFile() to explicitly send the file from your ASP.NET application and then add the Content Type and Content-Disposition headers.
For example:
Response.ContentType = "application/ms-excel";
Response.AppendHeader("Content-Disposition","attachment; filename=someFIle.xls");
Response.TransmitFile( Server.MapPath("~/somewhere/someFIle.xls") );
Response.End();
This will cause a Open / Save As dialog box to pop up with the filename of someFIle.xls as the default filename preset.
To force downloads, you have to set a couple of http headers. Content-Type and Content-Disposition. The first has to be application/octet-stream, and the second has to look something like this:
Content-Disposition: Attachment; Filename="[path to file user wants to download]"
Related
I am attempting to post a reply to an image board website.
It can be done through a winform web browser control or http request.
The issue is that with a post, you can upload an image with a input type file element on the page
<input type="file" />
For security reasons, I cannot set the value of the element to the file I want to upload.
When I use tamper data to see what is passed to the posting page, this is parameter that is passed under POST_DATA
-----------------------------256672629917035
Content-Disposition: form-data; name="upfile"; filename="image_file_name.jpg"
Content-Type: image/jpeg
So how is it possible to simulate a file upload of a input element in C#?
Look closely at the data that was posted, no directory is specified.
You can simulate any HTTP request through the HttpWebRequest object. Click here for an example.
When you're using the input type file element the browser is creating the request this for you. If you simply recreate this in HttpWebRequest you have full control over all aspects of the request and can modifiy as you wish.
It's probably in your interest to grab a copy of Fiddler.
If I understood your problem right,
you can't determine the directory of the file which was uploaded from your web browser control.
So ... it is impossible. The information about directory isn't given by browser - so it shouldn't be given by web browser control.
As MizardX suggested, it's better to send request using C#.
I have set up a WCF service. I have programmed the URL to be in the format:
http://localhost:64136/NewsProvider.svc/Rest/Getnews/1
When I set the return type to be XML I can change the number at the end of that link and browse the xml in a browser. However when I change it to JSON as soon as I hit the page in firefox I am asked do I want to download it?
How do I get round this and just view the pure JSON. When I used to write a page that wrote JSON I would add the following:
Response.Clear();
Response.ContentType = "text/plain";
Response.Write(json);
Response.End();
But I dont see how to change the content type and mess with the response object with WCF?
And will changing the content type actually let me just view the raw xml in the browser what govers this?
Yes firefox will offer you to open or download the content and if you do it you will see the passed JSON. If you don't like it you should install some Firefox extension (like JSONView) or HTTP proxy (like Fiddler).
I'm using a WebBrowser control in Silverlight and I'm setting it to a local HTML page. The HTML page has various links and they all work fine. Can I make it so that if the user clicks on an image file, it downloads to their system (or does the default behavior of the broweser) instead of displaying on the webpage? The main question is, is it possible to do this if I don't have access to the server itself? Thanks
edit - Is it possible to send an HTTPWebRequest to get the image and then edit the response headers, all from the client? This may be an alternative.
The standard way of doing this is to send the Content-Disposition HTTP header with attachment as the value. See here for more on this: Uses of content-disposition in an HTTP response header
But if you don't have access to the server, I don't think you can achieve this.
Have some audio and video files that users are to download, however depending on the file type or browser the browser may attempt to play the file instead of downloading it. This is not desired, how can I avoid this? The anchor will be a direct link to the file unless I need to create some sort of Action to handle this properly. I am using C# ASP.NET MVC.
The important parts are setting the response headers. Set both the content-type header and the content-disposition header. Here is an example:
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)
Response.AddHeader("Content-Length", lenOfFile)
Response.ContentType = "application/octet-stream"
You'll have to set the Content-Disposition HTTP header to 'attachment':
Content-Disposition: attachment; filename="file.ext"
I am working on a Custom WebPart, for which I need an application page to render a PDF file.
I am currently using following link http://support.microsoft.com/kb/306654
It works fine in ASP.NET, but gives a blank page in SharePoint.
Here's the code:
(PDF file is in same directory)
Response.ClearContent();
Response.ClearHeaders();
//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Get the physical path to the file.
string FilePath = MapPath("Test.pdf");
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
Response.Flush();
Response.End();
Working on it for last few hrs, making me crazy...any ideas?
A few ideas:
If the file is in the SharePoint directories, why not just redirect to a URL for it?
If you use Fiddler (or Firebug) to look at the HTTP traffic -- do you see the content-type change? What is the content (is it a PDF?)
The main differences between SharePoint and ASP.NET (assuming that you are getting to the file correctly) is that the page life-cycle is slightly different and the security model. You might want to look at how Reponse.End() works to make sure it isn't different in SharePoint. You can run SharePoint in verbose logging and then use .NET Reflector on the SharePoint dlls (with the logs as a guide) to see if there's some weird way of handling this. The SharePoint dlls have pretty liberal log calls. More info here: http://www.andrewconnell.com/blog/archive/2008/06/11/SharePoint-Debugging-and-Logging-Tips-and-Tricks.aspx
Have you considered rasterizing the PDF and just showing it inline in the page? This is what we do in a project my company is working on to view PDF and other documents in SharePoint (Vizit). You still have to solve your problem of reading the file, but instead of responding with the PDF, you respond with a PNG and request inside of an <img> tag.