作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试研究如何使用新的 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");
}
}
最佳答案
我没有真正尝试您的代码,但注意到一件事。难道你的 Response.ContentType = image/gif
流也应该是 BinaryStream 而不是常规流,因为它是你正在使用的图像......?
关于c# - .NET 4.5 HttpTaskAsyncHandler 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7796633/
public class FooHandler : HttpTaskAsyncHandler { public override async Task ProcessRequestAsync
出于 CORS 和安全目的,我们有一个网络应用程序通过 .NET IHttpHandler(称为 proxy.ashx)路由许多请求。一些资源加载速度快,而另一些资源加载速度慢,这取决于这些资源所需的
我正在尝试为我的自定义内容管理解决方案实现自定义 HttpTaskAsyncHandler。这个想法是将 /100/my-little-pony 路由到 /Details/my-little-pony
我正在尝试研究如何使用新的 asp.net 4.5 异步处理程序以及 Request.GetBufferlessInputStream 将图像上传到磁盘。此代码运行并写出一个文件,但图像已损坏,我不确
我是一名优秀的程序员,十分优秀!