Just when the application is still very eager to know when to receive the administrator agreed to open the notice, did not expect just a time when they began to tangle the first article to write something. I think, why don't you share your little piece of code?
1 varResponse =HttpContext.Current.Response;2Response.ContentType ="Application/octet-stream";3Response.AddHeader ("content-disposition","attachment; Filename="+ filename.substring (0, Filename.length-5) +". doc");4Response.WriteFile (Backup_file);//Backup_file is the previously defined file name.
In fact, this is very simple, but I studied for a long time, my own thinking is quite chaotic.
I thought of the ContentType property of Change HttpContext.Current.Response (hereinafter referred to as Response) as "Application/msword" and then Response () method to write the file to the HTTP response. The result failed, After the operation (home a button to submit a file to this handler.ashx, after processing form a Word document) browser downloaded the Handler.ashx file, open is garbled, from the size estimate, should this is the file I want, but the filename and extension is not what I want.
So I found an article on Baidu, referring to the use of response to tell the browser to download files, response contenttype should be: "Application/octet-stream", Using the same Response.WriteFile () method, I got the file I wanted.
At the same time, it is worth mentioning that I was using the backup_file variable, and its value is actually the physical path of the file on my host, this path is given to the method before the successful implementation of the requirements.
In Tool.oschina.net/commons's HTTP contenttype query, I get the result:
. * (binary stream, do not know the download file type) |
Application/octet-stream |
What I'm outputting is actually such a binary stream.
January 17, 2016, opened the blog Park's own blog