简体   繁体   English

Azure Blob存储-从WebApi控制器上载Http / StreamContent到CloudBlockBlob

[英]Azure Blob Storage - Uploading Http/StreamContent to a CloudBlockBlob from WebApi Controller

I am sending a PDF file to my WebAPI Controller via a POST request using Angular as such: 我正在使用Angular通过POST请求将PDF文件发送到WebAPI控制器:

  $scope.upload = Upload.upload({
        url: "../api/Locker/Upload", // webapi url
        method: "POST",
        file: controllerFile,
  })

which in my POST method on my controller I get the StreamContent of that file as follows: 在我的控制器上的POST方法中,我得到该文件的StreamContent,如下所示:

 public async Task<HttpResponseMessage> Post()
    {
        HttpRequestMessage request = this.Request;
        if (!request.Content.IsMimeMultipartContent())
        {
            request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
        }

        var result = await Request.Content.ReadAsMultipartAsync<CustomMultipartFormDataProvider>(new CustomMultipartFormDataProvider());

        string fileName = result.FileData[0].Headers.ContentDisposition.FileName;
        var fileData = result.Contents[0]; 

 }

It is saying that results.Contents[0] is of type HttpContent but in the Immediate window when I type fileData it says it is of StreamContent type. 就是说result.Contents [0]是HttpContent类型的,但是在即时窗口中,当我键入fileData时,它说它是StreamContent类型的。

I am trying to upload to Azure Blob Storage this fileData so that I can then retrieve it using a GET request later on, but am having trouble doing so. 我正在尝试将此fileData上传到Azure Blob存储,以便以后可以使用GET请求检索它,但是这样做很麻烦。

    //in post method
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(blobConnectionString);
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference(blobContainer);
    await container.CreateIfNotExistsAsync();

    string blobId = Guid.NewGuid().ToString();

    UploadToBlob(container, blobId, fileData);

and method where I am stuck: 和方法卡住了:

  private async void UploadToBlob(CloudBlobContainer container, string blobId, HttpContent fileData)
    {
        CloudBlockBlob block = container.GetBlockBlobReference(blobId);
        block.UploadFromStream(fileData);
    }

error on block.UploadFromStream because fileData is not a Stream of course. block.UploadFromStream错误,因为fileData当然不是Stream。

What can I do if I am expecting a HTTP Response with content being of type: arraybuffer so that I can expose the file in my web application such as: 如果期望HTTP响应的内容类型为arraybuffer,该怎么办,以便可以在Web应用程序中公开该文件,例如:

  //angular get request
 .success(function (data, status, headers, config) {
        // file is uploaded successfully
        console.log(data);

        var fileBack = new Blob([(data)], { type: 'application/pdf' });
        var fileURL = URL.createObjectURL(fileBack);
 }

那么block.UploadFromStream(await fileData.ReadAsStreamAsync())呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM