【发布时间】:2011-09-23 16:49:39
【问题描述】:
我已经构建了一个方法,它获取一个本地文件并将其发布到从 the 2nd answer here. 获取的远程站点
在远程站点上,我有我的 HttpHandler,但不知道文件字节在哪里,所以我可以将它保存在远程计算机上的某个位置。
有人可以帮助我了解如何在 HttpHandler 中使用该文件进行处理吗?我尝试了以下但 Request.Files 为空:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Specialized;
namespace Somewhere
{
public class UploadFileHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//VALIDATE FILES IN REQUEST
if (context.Request.Files.Count > 0)
{
//HANDLE EACH FILE IN THE REQUEST
foreach (HttpPostedFile item in context.Request.Files)
{
item.SaveAs(context.Server.MapPath("~/Temp/" + item.FileName));
context.Response.Write("File uploaded");
}
}
else
{
//NO FILES IN REQUEST TO HANDLE
context.Response.Write("No file uploaded");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
【问题讨论】:
-
Request.Files不应为空。如果你制作一个只上传文件的小 HTML 页面,并让它将文件发送给你的处理程序,Request.Files还是是空的吗? -
为了让这个问题对未来的访问者有用,请解释为什么接受的答案解决了您的问题。 :)
标签: c# asp.net forms http-post httphandler