To use ASP.net to implement the attachment download function in the mailbox

Source: Internet
Author: User
Tags tostring
asp.net| download did not straightened for a long time, there are many cases on the Internet are the same: IE supported files will be opened directly, and other files such as zip and other documents do not recognize, will pop up to download or open the dialog box.
Suddenly think is not what the problem of file screening, sure enough to write the file before the filter can be used as an attachment.

VB.net:
Dim filename as String = "A.txt"

If filename <> "" Then

Dim path as String = Server.MapPath (filename)

Dim file as System.IO.FileInfo = New System.IO.FileInfo (path)

If file. Exists Then

Response.Clear ()

Response.AddHeader ("Content-disposition", "attachment; Filename= "+ file. Name)

Response.AddHeader ("Content-length", file. Length.tostring ())

Response.ContentType = "Application/octet-stream"

Response.Filter.Close ()

Response.WriteFile (file. FullName)


Response.End ()

Else

Response.Write ("This file does not exist.")

End If

End If

----------------------------------------------------------------
C#:

string filename = "A.txt";

if (filename!= "")
{

String path = Server.MapPath (filename);

System.IO.FileInfo file = new System.IO.FileInfo (path);

if (file. Exists)
{

Response.Clear ();

Response.AddHeader ("Content-disposition", "attachment; Filename= "+ file. Name);

Response.AddHeader ("Content-length", file. Length.tostring ());

Response.ContentType = "Application/octet-stream";

Response.Filter.Close ();

Response.WriteFile (file. FullName);


Response.End ();

}

Else
{

Response.Write ("This file does not exist.");

}

}




Related Article

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.