我已經構建,需要一個本地文件並將其發佈到從the 2nd answer here.如何通過HttpHandler使用發佈的文件?
採取遠程站點遠程站點,我有一個方法,我的HttpHandler的,但不知道該文件的字節,所以我可以保存在某個地方它在遠程機器上。
有人可以幫助我如何使用HttpHandler中的文件進行處理嗎?我試過以下,但Request.Files是空的:在HttpRequest
從上下文的
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' *仍然是空的? – bzlm
爲了讓這個問題對未來的訪問者有用,請解釋爲什麼接受的答案解決了您的問題。 :) – bzlm