我正在使用Angular通过POST请求将PDF文件发送到我的WebAPI控制器:
$scope.upload = Upload.upload({
url: "../api/Locker/Upload", // webapi url
method: "POST",
file: controllerFile,
})
在我的控制器的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];
}
它说results.Contents [0]的类型为HttpContent,但是当我输入fileData时,它在立即窗口中表示它是StreamContent类型。
我正在尝试将此文件数据上传到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);
和我陷入困境的方法:
private async void UploadToBlob(CloudBlobContainer container, string blobId, HttpContent fileData)
{
CloudBlockBlob block = container.GetBlockBlobReference(blobId);
block.UploadFromStream(fileData);
}
block.UploadFromStream上的错误,因为fileData当然不是Stream。
如果我期待内容类型为:arraybuffer的HTTP响应,我可以做什么,以便我可以在我的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);
}
答案 0 :(得分:2)
block.UploadFromStream(await fileData.ReadAsStreamAsync())怎么样?