2017-07-19 53 views
1

我創建了一個自定義的MultipartStreamProvider,將文件作爲電梯的一部分存儲在Azure文件存儲中&傳統應用程序的移位工作。客戶端在前端使用AngularJS,在後端使用WebAPI。當我嘗試使用MultipartStreamProvider時,我需要實現GetStream以返回要寫入的流。我正在使用cloudFile.OpenWrite,它要求將寫入它的流/文件的大小。但是,在GetStream中,ContentDisposition.Size是空的。有沒有一種方法可以讓AngularJS發送每個文件或後端的內容大小,也許我可以從其他地方挖掘文件流的大小?任何幫助將不勝感激。謝謝!如何獲取MultipartStreamProvider中的文件大小當從ContentDisposition中丟失時的GetStream

MultipartStreamProvider

 public override Stream GetStream(HttpContent parent, HttpContentHeaders headers) 
    { 
     // For form data, Content-Disposition header is a requirement 
     ContentDispositionHeaderValue contentDisposition = headers.ContentDisposition; 

     Console.WriteLine(files.Count); 
     if (contentDisposition != null) 
     { 
      // create default filename if its missing 
      contentDisposition.FileName = (String.IsNullOrEmpty(contentDisposition.FileName) ? $"{Guid.NewGuid()}.data" : contentDisposition.FileName); 

      // We won't post process files as form data 
      _isFormData.Add(false); 

      CloudMultipartFileData fileData = new CloudMultipartFileData(headers, _fileRepository.BaseUrl, contentDisposition.FileName);// your - aws - filelocation - url - maybe); 
      _fileData.Add(fileData); 

      var azureStream = _fileRepository.GetWriteStream(contentDisposition.Size, _relativeDirectory, fileData.FileName); 

      return azureStream; 

      // We will post process this as form data 
      _isFormData.Add(true); 
     } 

     throw new InvalidOperationException("Did not find required 'Content-Disposition' header field in MIME multipart body part.."); 
    } 

實際通話天青

 public override Stream GetWriteStream(long? fileSize, string relativeDirectory, string filename) 
    { 
     var combinedRelativeDirectory = GetCloudDirectory(relativeDirectory); 
     CloudFile cloudFile = combinedRelativeDirectory.GetFileReference(filename); 
     return cloudFile.OpenWrite(fileSize, null, null); 
    } 

AngularJS文件上傳代碼

 /********************************** Add/Upload Photos **************************************/ 
    $scope.$watch('files', function (files) { 
     $scope.formUpload = false; 
     console.log(files); 
     if (files != null) { 
      for (var i = 0; i < files.length; i++) { 
       $scope.errorMsg = null; 
       (function (file) { 
        upload(file); 
       })(files[i]); 
      } 
     } 
    }); 

    function upload(file) { 
     file.upload = Upload.upload({ 
      url: window.location.origin + "/api/mydocs/uploadfile?storeFolder=" + $scope.attachmentFolder + "&storeId=" + $scope.storeId + "&userId=" + $scope.currentUser.UserId, 
      method: 'POST', 
      headers: {}, 
      fields: {}, 
      file: file 

     }); 

回答

0

我結束了手動添加文件大小頭

 function upload(file) { 
     file.upload = Upload.upload({ 
      url: window.location.origin + "/api/mydocs/uploadfile?storeFolder=" + $scope.myFolder + "&clientId=" + $scope.clientId, 
      method: 'POST', 
      headers: { 'file-info':file.name + "-/" + file.size }, 
      fields: {}, 
      file: file 

     }); 

然後在構造函數中,我用它來創建一個查找表:

public MyCloudMultipartFormDataStreamProvider(string relativeDirectory, IEnumerable<string> lookupInfo) 
    { 
     NewFileNames = new Dictionary<string, string>(); 

     _fileRepository = new CloudFileRepository(); 
     _relativeDirectory = relativeDirectory; 
     _uploadedFilesLookup = new Dictionary<string, long>(); 
     foreach (var fileInfo in lookupInfo) 
     { 
      var values = Regex.Split(fileInfo, @"-/"); 
      _uploadedFilesLookup.Add(values[0], Int64.Parse(values[1])); 
     } 
    } 

然後抓住該文件的大小進行查找的表並將其傳遞給我的GetWriteStream方法

  var azureStream = _fileFacade.GetWriteStream(_uploadedFilesLookup[fileName], 
       _relativeDirectory, fileData.FileName, out newFileName); 
相關問題