gpt4 book ai didi

c# - 如何将 MultipartMemoryStreamProvider 的文件内容作为字节数组获取?

转载 作者:太空狗 更新时间:2023-10-30 00:41:35 25 4
gpt4 key购买 nike

我创建了一个使用 FormData 将文件上传到 Web API Controller 的组件。

如何从 MultipartMemoryStreamProvider 获取字节数组形式的文件内容?

这里是Web Api方法

   public Task<IEnumerable<FileModel>> Post()
{
if (Request.Content.IsMimeMultipartContent())
{
var streamProvider = new MultipartMemoryStreamProvider();
var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith<IEnumerable<FileModel>>(t =>
{

if (t.IsFaulted || t.IsCanceled)
{
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}

FileDataBO filedata;
var fileInfo = streamProvider.Contents.Select(i => {
//save to db
filedata = new FileDataBO ();
filedata.FileName = i.Headers.ContentDisposition.FileName;
filedata.FileType = "jpeg";

// HOW TO GET FILE CONTENT HERE??? IT SHOULD BYTE[]
//filedata.FileContent = ???

//TODO
//_fileDataService.SaveFile(filedata);

return new FileModel(i.Headers.ContentDisposition.FileName, 2048);
});
return fileInfo;
});

return task;
}
else
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
}
}

最佳答案

您应该能够通过执行 i.ReadAsByteArrayAsync() 获取内容

关于c# - 如何将 MultipartMemoryStreamProvider 的文件内容作为字节数组获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19710812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com