Download a file stored in database - asp.net

I have file content in sql database as binary format and also saved the file with its extension .
I want to have pop up for save as ,so that client can save the file on her system in the same document format.
i.e if it .doc then it should get save in .doc format only.
Please provide the code for the same.

You need to
Set up IIS to to handle files of the types you want. For example, you need to map .pdf, .jpg, etc, to be handled by the ASP.net dll. Also is important to make sure "Verify file exists" is off: http://msdn.microsoft.com/en-us/library/bb515343.aspx
Create a HTTP Handler for the files to retrieve the binary from the database and .BinaryWrite it out to the output stream: http://www.developer.com/net/asp/article.php/3565541/Use-Custom-HTTP-Handlers-in-Your-ASPNET-Applications.htm
Set up your web.config file to map the file types required to the handler you created (this is detailed in the url provided for point 2)
You say that you want it to open as a download box, and not just open in the browser. To do this you have to use the content-disposition HTTP header. So in your custom handler you write, make sure to add a header specifiying the content-disposition as attachment. http://support.microsoft.com/kb/260519 will tell you what you need.
I want to have pop up for save as ,so
that client can save the file on her
system in the same document format.
i.e if it .doc then it should get save
in .doc format only.
You take care of this when you write out the content disposition HTTP header. Refer to the MS link I provided and make sure you specify a filename with the correct extension.
Also note the comments above. If this helps you, you need to accept it as the answer. Also go back and accept some answers on your older questions. Otherwise people are not inclined to help you. I will help you again if you mark this as answer, or leave me a comment about why it does not answer your question.
Good luck
bgs264

Related

Ask in a web form for the path of a file accessed from a user's computer

In a web form (aspx) I want to ask the user for the path of a file that needs to be used by one of our team.
At first it was a simple textbox but a lot of people send us wrong path (copy/paste seemed too hard for them).
So I tried to use an input file to be sure the path exists but noticed that the file is then send with the form. As the files can weight hundreds of Mb that was not acceptable.
Then I found a way with Jquery to use the input file to get the path of the file without sending it : it works fine on IE but Chrome translate the path in "fakepath". I understand that it is for security reasons.
So I am again looking for a solution, working on all browser, to get from the user the path of a file (with a specific extension) without him being able to write a wrong path.
I hope someone will have the brillant idea I am unable to find :-).
How about receiving only a few kilobytes of the file data from stream instead of the whole file to ensure the path points to a valid file.

Get file path & filename with asp:FileUpload, don't want file... just path and name

I am developing a .NET intranet site which will enable the user to see a list of files (file details stored in DB) and link to the actual PDF/XML/XLS and open it... kind of like a table of contents for the network.
During data entry, the user enters various data about a document, then browses to the file on the network and selects it using the asp:FileUpload. The codebehind then saves the network path to the DB. There is alot of overhead here because i'm sending the file to the server but never use it.
Everything has been working fine until someone tries to use a large PDF file then I get the dreaded MAXIMUM REQUEST LENGTH EXCEEDED error... So I'm trying to find a solution here... I do not need the actual file.. just the path and filename.
I know not all browsers send the full path but our systems have older browsers so everything is working fine now, but will probably break soon.. which is another reason to find a different solution.
I've looked into Javascript to pull the path but that won't work...
Any other ideas? Other ways to just grab the path and filename? (besides manually typing it in to a Text field)
Thanks,
Todd.
This may help too
How to get the full path of a file from asp: file upload?
string filename = Path.GetFileName(FileUpload1.FileName);//file name
string path= Server.MapPath(filename);//path

Determining .exe file in time of upload

I have developed File Upload web page in ASP.NET. Now user can rename a .exe file to txt or some other extension and upload the same. I want to restrict that. How I can implement that in ASP.NET?
The only safe way to do this is to get the byte [] from the file that has been posted and examine it to determine if the file is indeed in one of the formats you allow the user to upload. You don't need to save the file, you can just get the byte[] from the HttpPostedFile object.
Other than examining the content (looking for magic numbers, for example) there isn't an infallible way to make sure that the user is not attempting to upload something that you don't allow.

How download manager breaks the file into multiple parts?

I am not clear about the concept of breaking the file into multiple part and then download each part separately. According to me, What we have only the path of that file where it exist on the internet so how to break this file just by knowing the URL or path?
There is a special provision in HTTP 1.1 for this: the Range header, which allows you to fetch a selected portion of the resource. This is exactly what these download managers use.
You can review some code example in Java of partial file download: Resume download in urlconnection. In rfc2616 specified header 'Range' allows to request specified part of file.
So Download Manager simply start partial file downloading in several connection in parallel.

How to display show message to user after Response.close?

In my asp.net web application i have to convert resx file to excelfile and then i should provide an option to download the converted file. I have done the download function using response.Addheader method. Now i wanted to display statics to the user of how many keys are converted from resx file to excel file.
I have placed an label to display no of keys migrated but the code is not exceuted after response.end. Pls help me to get this done
Thanks
Rm
Short answer is that you cannot send some output once the response has closed.
Now to achieve what you want to do, you have to emit statistics along with link to download the actual excel file. For example,
Convert the file and store results into file system. You should use some random key (such as guid) for generating the file name.
Output statistics that you want to show to the user.
In the same output, emit a start-up java-script that would redirect the browser to the url that will download the file generated in step1 - the file name key will useful for creating such URL.
In rare cases where JS doesn't work or re-direct takes more time, The output from #2 should also contain a link that will allow user to download the file manually (the link will be accompanied with some friendly message)

Resources