C # downloads based on path file address

Source: Internet
Author: User
Tags decrypt

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

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.