uploadFileOss.aspx.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/";
  32. WebClient webClient = new WebClient();
  33. if (!Directory.Exists(filePath))
  34. {
  35. Directory.CreateDirectory(filePath);
  36. }
  37. filePath += fileName;
  38. webClient.DownloadFile(file_url, filePath);
  39. jiemiUtils.ossFileDecrypt(filePath);
  40. FileStream fs = new FileStream(filePath, FileMode.Open);
  41. byte[] bytes = new byte[(int)fs.Length];
  42. fs.Read(bytes, 0, bytes.Length);
  43. fs.Close();
  44. Response.Clear();
  45. //
  46. Response.ClearContent();
  47. Response.ClearHeaders();
  48. Response.ContentType = "application/octet-stream";
  49. //通知浏览器下载文件而不是打开\\fileDownload=true; path=/
  50. Response.AddHeader("Set-Cookie", "fileDownload=true; path=/;");
  51. //Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
  52. Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
  53. Response.AddHeader("Content-Length", bytes.Length.ToString());
  54. Response.AddHeader("Access-Control-Expose-Headers", "Content-Disposition");
  55. Response.BinaryWrite(bytes);
  56. Response.Flush();
  57. Response.End();
  58. File.Delete(filePath);
  59. return;
  60. }
  61. catch (Exception ex)
  62. {
  63. Response.Write("{\"res\":\"0\",\"msg\":\"发生错误!" + ex.Message + "!\"}");
  64. Response.End();
  65. return;
  66. }
  67. }
  68. }