January 17, 2016, opened the blog Park's own blog

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.