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.