Download an object in ASP. NET through a dialog box

Source: Internet
Author: User

1. You are prompted to download or open the file in the pop-up dialog box.

2. allow specific applications through custom HeadersProgramOpen a file

Usage: response. transmitfile ()

Routine:
Response. contenttype = "image/JPEG ";
Response. appendheader ("Content-disposition", "attachment; filename=sailbig.jpg ");
Response. transmitfile (server. mappath ("~ /Images/sailbig.jpg "));

 
The method used for stream transmission:

Response. binarywrite () and response. outputstream ()

Routine:

Bitmap BMP = wwwebutils. cornerimage (backcolor, color, C, radius, height, width );
Response. contenttype = "image/JPEG ";
Response. appendheader ("Content-disposition", "attenment; filename=leftcorner.jpg ");
BMP. Save (response. outputstream, imageformat. JPEG );

Reference URL for content type (MIME type:

Http://www.w3.org/TR/html4/types.html (Overview)

Http://www.iana.org/assignments/media-types/ (detail list)

Solutions to common problems:

1. An error occurs when an image is loaded from a resource file or Database BLOB field.

Error message: a generic error occurred in GDI +

Code:

Bitmap BMP = This. getglobalresourceobject ("resource", "_ bitmap") as bitmap;
Response. contenttype = "image/JPEG ";
BMP. Save (response. outputstream, imageformat. JPEG );
Response. End ();

Solution: Create an instance to receive the image content read from the resource file or Database BLOB field.

Solution code:

Bitmap BMP = This. getglobalresourceobject ("resource", "_ bitmap") as bitmap;
Bitmap temp = new Bitmap (BMP );
Response. contenttype = "image/JPEG ";
Temp. Save (response. outputstream, imageformat. JPEG );
BMP. Dispose ();
Temp. Dispose ();
Response. End ();

 
2. The PNG image cannot be directly stored in the output stream.

Cause: PNG is a special image format.

Solution code:

Bitmap BMP = This. getglobalresourceobject ("resource", "_ bitmap") as bitmap;
Bitmap temp = new Bitmap (BMP );
Memorystream MS = new memorystream ();
Response. contenttype = "image/PNG ";
Temp. Save (MS, system. Drawing. imaging, imageformat. PNG );
Ms. writeto (response. outputstream );
BMP. Dispose ();
Temp. Dispose ();
Response. End ();

 

3. Solve cache Problems

Response. contenttype = "image/PNG ";
Response. Buffer = false;
Response. Clear ();
Memorystream stream1 = new memorystream ();
// Drawpie method return an image
This. drawpie (Table1). Save (stream1, imageformat. PNG );
Response. binarywrite (stream1.toarray ());
Base. onpreinit (E );
 

 

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.