使用AWSSDK.S3 nuget包,我试图返回一个已从S3桶中检索的文件。我的出发点是基于这个SO answer中给出的例子。
示例控制器代码:
public FileResult GetFile(Guid id)
{
// no using block as it will be disposed of by the File method
var amazonResponse = _foo.GetAmazonResponseWrapper(_user, id);
// set Response content-length header
// set Response content-type header
var bufferSize = 1024;
var buffer = new byte[bufferSize];
int bytesRead;
while ((bytesRead = amazonResponse.ResponseStream.Read(buffer, 0, buffer.Length)) > 0 && Response.IsClientConnected)
{
Response.OutputStream.Write(buffer, 0, bytesRead);
Response.OutputStream.Flush();
buffer = new byte[bufferSize];
}
// this will not work (can't read from this stream)
return File(Response.OutputStream, "text/plain", "bar.txt");
}如果我写到我在while循环中创建和使用的一个while,我将得到一个文件,但是不会有任何内容。
我发现获取文件中内容的唯一方法是调用流上的.ToArray(),如下所示:
return File(memStream.ToArray(), "text/plain", "foo.txt");是否有一种方法可以将文件实际流到浏览器,而不将其加载到web服务器的内存中?
发布于 2017-10-05 22:34:26
把小溪往前流过
public FileResult GetFile(Guid id) {
// no using block as it will be disposed of by the File method
var amazonResponse = _foo.GetAmazonResponseWrapper(_user, id);
return File(amazonResponse.ResponseStream, "text/plain", "bar.txt");
}您已经显示了可以从响应流中读取。然后,只需传递响应流,文件结果将读取它并返回响应。
https://stackoverflow.com/questions/46595222
复制相似问题