uploadFileOss.aspx.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using BizCom;
  2. using NPOI.SS.Formula.Functions;
  3. using SiteCore;
  4. using SiteCore.taobao;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Text;
  11. using System.Web;
  12. using System.Web.UI;
  13. using System.Web.UI.WebControls;
  14. using Utils.ImageUtils;
  15. public partial class plug_uploadFileOss : System.Web.UI.Page
  16. {
  17. protected void Page_Load(object sender, EventArgs e)
  18. {
  19. string file_url = Request["url"];
  20. try
  21. {
  22. if (file_url == null)
  23. {
  24. Response.Write("{\"res\":\"0\",\"msg\":\"错误的下载访问!\"}");
  25. Response.End();
  26. return;
  27. }
  28. Uri uri = new Uri(file_url);
  29. // 获取路径中的文件名(包含后缀)
  30. string fileName = System.IO.Path.GetFileName(uri.AbsolutePath);
  31. string filePath = "D://temp/" + fileName;
  32. WebClient webClient = new WebClient();
  33. if (Directory.Exists(filePath))
  34. {
  35. Directory.CreateDirectory(filePath);
  36. }
  37. webClient.DownloadFile(file_url, filePath);
  38. jiemiUtils.ossFileDecrypt(filePath);
  39. FileStream fs = new FileStream(filePath, FileMode.Open);
  40. byte[] bytes = new byte[(int)fs.Length];
  41. fs.Read(bytes, 0, bytes.Length);
  42. fs.Close();
  43. Response.Clear();
  44. //
  45. Response.ClearContent();
  46. Response.ClearHeaders();
  47. Response.ContentType = "application/octet-stream";
  48. //通知浏览器下载文件而不是打开\\fileDownload=true; path=/
  49. Response.AddHeader("Set-Cookie", "fileDownload=true; path=/;");
  50. //Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
  51. Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
  52. Response.AddHeader("Content-Length", bytes.Length.ToString());
  53. Response.AddHeader("Access-Control-Expose-Headers", "Content-Disposition");
  54. Response.BinaryWrite(bytes);
  55. Response.Flush();
  56. Response.End();
  57. File.Delete(filePath);
  58. return;
  59. }
  60. catch (Exception ex)
  61. {
  62. Response.Write("{\"res\":\"0\",\"msg\":\"发生错误!" + ex.Message + "!\"}");
  63. Response.End();
  64. return;
  65. }
  66. }
  67. }