1: Write encryption method in JS file
var Comm = {
Encode:function (text) {
if (text)
return new Base64 (). Encode (text);
Else
return text;
}
}
Return file address in 2:handler class:
such as: string path = "/files/trial submissions. doc";
3: In the JS method to receive and jump to the page to download
function Download () {
$.post (', {action: '},function (data) {
if (path!= "") {
Window.location.href= "/download.aspx? p= "+comm.encode (path);
}
});
}
4: Background code operation also in Download.aspx
A: First write the decryption method
public static string Decrypt (string text) {
byte[] bytes = convert.frombase64string (text);
Return Encoding.UTF8.GetString (bytes);
}
B: File parsing and download operations
protected void Page_Load (object sender, EventArgs e) {
String path = Decrypt (parse.tostring (request["P"]). Replace ("", "+"));
String fileName = path. Substring (path. LastIndexOf ('/') + 1);
var FilePath = HttpContext.Current.Server.MapPath (path);
try {
Determine if this file exists
if (file.exists (FilePath)) {
FileStream io = File.Open (FilePath, FileMode.Open);
Long fileSize = io. Length;
byte[] Filebit = new byte[(int) fileSize];
Io. Read (filebit, 0, (int) fileSize);
Response.Clear ();
filename = httputility.urlencode (filename);
Response.ContentType = "Application/octet-stream";
Response.AddHeader ("Content-disposition", "attachment;filename=" + fileName);
Response.BinaryWrite (Filebit);
Io. Dispose ();
Response.Flush ();
Response.End ();
} else {
Response.Clear ();
Response.Write ("<script type=\" text/javascript\ "src=\"/scripts/jquery-1.10.2.js\ "></script>< script>$ (function () {alert (' file does not exist or has been deleted ', {icon:0}, function () {window.close ()}, function () {window.close ();} );}); </script> ");
}
}catch (Exception ex) {
Response.Clear ();
Response.Write ("<script type=\" text/javascript\ "src=\"/scripts/jquery-1.10.2.js\ "></script> < script>$ (function () {alert (' "+ Ex.) Message + "', {icon:0}, function () {window.close ()}, function () {window.close ();});}); </script> ");
}
}
C # downloads based on path file address