Use Jquery Ajax to download instance code

Source: Internet
Author: User
Tags flush

You can use jquery ajax to submit a form request.

Jquery download function:

The code is as follows: Copy code
// Download an Ajax file
JQuery. download = function (url, data, method ){
// Obtain url and data
If (url & data ){
// Data is string or array/object
Data = typeof data = 'string '? Data: jQuery. param (data );
// Assemble parameters into form input
Var inputs = '';
JQuery. each (data. split ('&'), function (){
Var pair = this. split ('= ');
Inputs + = '<input type = "hidden" name = "' + pair [0] + '" value = "' + pair [1] + '"/> ';
});
// Request to send a request
JQuery ('<form action = "' + url + '" method = "' + (method | 'post ') + '">' + inputs + '</form> ')
. AppendTo ('body'). submit (). remove ();
};
};

Organize a string in jquery mode and simulate submitting a form request.

That is, to dynamically render the form, submit the form and then delete it.


Html image code:

The code is as follows: Copy code



Call the GetSrcFromSvc function:


$. Download ("http: // localhost: 2204/wx/Default. aspx", "img =" + url, 'post ');

Asp.net server code: aspx file:

Microsoft provides a new TransmitFile method for the Response object to solve the problem of using Response. BinaryWrite to download more
Mbit/s file causes the aspnet_wp.exe process to recycle and fail to download the file. /// Specify the address of the output image
 

The code is as follows: Copy code
String imgurl = Request. Form ["img"];
String FileName = Server. MapPath (imgurl );
// System. Drawing. Image img = System. Drawing. Image. FromFile (imgurl );
// MemoryStream MS = new System. IO. MemoryStream ();
// Img. Save (MS, System. Drawing. Imaging. ImageFormat. Jpeg );
// Img. Dispose ();
// Context. Response. ClearContent ();
// Context. Response. ContentType = "image/jpg ";
// Context. Response. BinaryWrite (ms. ToArray ());
/// Context.htm = htm & File (FileName );
//////?? All streams in uffer are sent
// Context. Response. Flush ();
/// Context. Response. End ();
String filePath = Server. MapPath (imgurl); // path
If (File. Exists (filePath ))
{
FileInfo fileinfoo = new FileInfo (filePath );
Response. ContentType = "application/x-zip-compressed ";
Response. AddHeader ("Content-Disposition", "attachment; filename =" + fileinfoo. Name + "");
Response. TransmitFile (filePath );
}
Else
{
Htm = htm & ("file not found. ");
}

Asp.net stream download:

The code is as follows: Copy code
String imgurl = Request. Form ["img"];
String FileName = Server. MapPath (imgurl );
If (File. Exists (FileName ))
{
FileInfo fileinfoo = new FileInfo (FileName );
// Download an object in the form of a streaming
FileStream fs = new FileStream (FileName, FileMode. Open );
Byte [] bytes = new byte [(int) fs. Length];
Fs. Read (bytes, 0, bytes. Length );
Fs. Close ();
Response. ContentType = "application/octet-stream ";
// Notify the browser to download the file instead of opening it
Response. AddHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. UrlEncode (fileinfoo. Name, System. Text. Encoding. UTF8 ));
Response. BinaryWrite (bytes );
Response. Flush ();
Response. End ();
}

Test environment:

Win7 + IE9 IE10. Mobile phone: uc.

Other browsers cannot predict the effect.

 

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.