.NET 4.5 HttpTaskAsyncHandler上传文件
我正在尝试弄清楚如何使用新的 asp.net 4.5 异步处理程序以及 Request.GetBufferlessInputStream 将图像上传写入磁盘。这段代码运行并写出一个文件,但图像已损坏,我不知道为什么。这是我正在使用的代码
public class UploadHandler : HttpTaskAsyncHandler
{
public override Task ProcessRequestAsync(HttpContext context)
{
// Gets a Stream object that can be used to read the
// incoming HTTP entity body, optionally disabling the
// request-length limit that is set in the MaxRequestLength property.
// This method provides an alternative to using the
// InputStream property. The InputStream property waits until the
// whole request has been received before it returns a Stream object.
// In contrast, the GetBufferlessInputStream method returns
// the Stream object immediately.
// You can use the method to begin processing the
// entity body before the complete contents of the
// body have been received.
// The entity body (or as much of it as you request and has
// been received) is returned only when you use the object that
// is returned by this method to read the stream, by calling
// methods such as the Read method. You use parameters of the
// Read method to specify how much of the entity body to read.
// This method can be useful if the request is uploading a
// large file and you want to begin accessing the file contents
// before the upload is finished.
// However, you should only use this method for scenarios where
// you want to take over all processing of the entity body.
// This means that you cannot use this method from an .aspx page,
// because by the time an .aspx page runs, the entity body
// has already been read.
using (Stream input = context.Request.GetBufferlessInputStream(true))
using (var file = new FileStream("C:\\myfile.jpg", FileMode.Create,
FileAccess.Write, FileShare.Write))
{
input.CopyTo(file);
}
context.Response.ContentType = "text/plain";
return context.Response.Output.WriteAsync("Done");
}
}
I am trying to work out how to use the new asp.net 4.5 async handlers as well as the Request.GetBufferlessInputStream to write a image upload out to disk. This code runs and it writes a file out but the image is corrupt and I am not sure why. Here is the code I am using
public class UploadHandler : HttpTaskAsyncHandler
{
public override Task ProcessRequestAsync(HttpContext context)
{
// Gets a Stream object that can be used to read the
// incoming HTTP entity body, optionally disabling the
// request-length limit that is set in the MaxRequestLength property.
// This method provides an alternative to using the
// InputStream property. The InputStream property waits until the
// whole request has been received before it returns a Stream object.
// In contrast, the GetBufferlessInputStream method returns
// the Stream object immediately.
// You can use the method to begin processing the
// entity body before the complete contents of the
// body have been received.
// The entity body (or as much of it as you request and has
// been received) is returned only when you use the object that
// is returned by this method to read the stream, by calling
// methods such as the Read method. You use parameters of the
// Read method to specify how much of the entity body to read.
// This method can be useful if the request is uploading a
// large file and you want to begin accessing the file contents
// before the upload is finished.
// However, you should only use this method for scenarios where
// you want to take over all processing of the entity body.
// This means that you cannot use this method from an .aspx page,
// because by the time an .aspx page runs, the entity body
// has already been read.
using (Stream input = context.Request.GetBufferlessInputStream(true))
using (var file = new FileStream("C:\\myfile.jpg", FileMode.Create,
FileAccess.Write, FileShare.Write))
{
input.CopyTo(file);
}
context.Response.ContentType = "text/plain";
return context.Response.Output.WriteAsync("Done");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有真正尝试你的代码,我注意到一件事。您的 Response.ContentType = image/gif 流不应该也是 BinaryStream 而不是常规流,因为它是您正在使用的图像..?
not really trying your code out I noticed one thing. Shouldn't your
Response.ContentType = image/gif
also should the stream be a BinaryStream instead of a regular stream since it's an Image that you are working with..?看来现在有一种简单的方法可以使用 ASP.NET Web Api 来处理这个问题!
请在此处阅读:http://blogs.msdn.com/b/henrikn/archive/2012/03/01/file-upload-and-asp-net-web-api.aspx
使用一个这些坏男孩 MultipartFormDataStreamProvider
Well it looks like there is an easy way to handle this now using ASP.NET Web Api!
Read about it here: http://blogs.msdn.com/b/henrikn/archive/2012/03/01/file-upload-and-asp-net-web-api.aspx
Use one of these bad boys MultipartFormDataStreamProvider