UploadContent.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Http;
  5. using System.Net.Http.Headers;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using WingInterfaceLibrary.DTO.Report;
  9. using WingServerCommon.Log;
  10. namespace WingAIDiagnosisService.Manage
  11. {
  12. public class UploadContent : HttpContent
  13. {
  14. private const int ChunkSize = 2097152;//2M;
  15. private readonly Stream _uploadStream;
  16. private readonly long _fileSize;
  17. public event EventHandler<double> ProgressChanged;
  18. public UploadContent(string filePath)
  19. {
  20. if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath))
  21. {
  22. throw new Exception("UploadFile not find");
  23. }
  24. var fileName = Path.GetFileName(filePath);
  25. _uploadStream = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  26. var mimeType = FileHelper.GetMimeType(fileName.Substring(fileName.LastIndexOf(".")));
  27. Headers.ContentType = MediaTypeHeaderValue.Parse(mimeType);
  28. Headers.ContentLength = _uploadStream.Length;
  29. _fileSize = _uploadStream.Length;
  30. }
  31. public UploadContent(byte[] fileData, MediaTypeHeaderValue mediaTypeHeaderValue)
  32. {
  33. if (fileData == null || fileData.Length == 0)
  34. {
  35. throw new Exception("FileData is empty");
  36. }
  37. _uploadStream = new MemoryStream(fileData);
  38. Headers.ContentType = mediaTypeHeaderValue;
  39. Headers.ContentLength = _uploadStream.Length;
  40. _fileSize = _uploadStream.Length;
  41. }
  42. /// <summary>
  43. /// 将内容写入流
  44. /// </summary>
  45. /// <param name="stream"></param>
  46. /// <param name="context"></param>
  47. /// <returns></returns>
  48. protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
  49. {
  50. for (long i = 0; i < _uploadStream.Length; i += ChunkSize)
  51. {
  52. var dataToWrite = new byte[Math.Min(ChunkSize, _uploadStream.Length - i)];
  53. await _uploadStream.ReadAsync(dataToWrite, 0, dataToWrite.Length);
  54. await stream.WriteAsync(dataToWrite, 0, dataToWrite.Length);
  55. OnProgressChanged((double)(i * 100 / _uploadStream.Length));
  56. }
  57. OnProgressChanged(100);
  58. }
  59. /// <summary>
  60. /// 比较文件大小
  61. /// </summary>
  62. /// <returns></returns>
  63. public bool CompareFileSizes(long webFileSize)
  64. {
  65. return _fileSize == webFileSize;
  66. }
  67. protected override bool TryComputeLength(out long length)
  68. {
  69. length = _uploadStream.Length;
  70. return true;
  71. }
  72. private void OnProgressChanged(double e)
  73. {
  74. ProgressChanged?.Invoke(this, e);
  75. }
  76. public new void Dispose()
  77. {
  78. base.Dispose();
  79. _uploadStream.Close();
  80. ProgressChanged = null;
  81. }
  82. }
  83. }