2014-04-17 31 views
0

我必須創建一個接收多部分文件數據並將其保存到特定位置的剃鬚刀文件。 我之前在.ashx文件中完成了此操作。像這樣在剃鬚刀文件中接收多部分文件數據

<%@ WebHandler Language="C#" Class="Handler" %> 
using System; 
using System.Web; 

public class Handler : IHttpHandler { 

public void ProcessRequest (HttpContext context) { 
    context.Response.ContentType = "text/plain"; 
    if (context.Request.Files.Count > 0) 
    { 
     for (int i = 0; i < context.Request.Files.Count; i++) 
     { 
      string fileNamePath = System.IO.Path.GetFileNameWithoutExtension(context.Request.Files[i].FileName) + "_" + System.Guid.NewGuid().ToString("N") + System.IO.Path.GetExtension(context.Request.Files[i].FileName); 
      string savePath = context.Server.MapPath("~/media/mm-signup-files/") + fileNamePath; 
      context.Request.Files[i].SaveAs(savePath); 

      context.Response.Write(fileNamePath + "|" + System.IO.Path.GetFileName(context.Request.Files[i].FileName)); 


     } 

    } 

} 

public bool IsReusable { 
    get { 
     return false; 
    } 
} 

}

但現在我想要實現在剃刀文件的代碼。是否有可能把它變成剃刀? 我不明白如何得到HttpContext剃鬚刀文件與實施IHttpHandler接口。或者我錯過了任何東西?

回答

1

的HttpContext是剃刀網頁的屬性,以便您可以訪問的響應和請求本身:

@{ 
    if (Request.Files.Count > 0) 
    { 
     for (int i = 0; i < Request.Files.Count; i++) 
     { 
      string fileNamePath = Path.GetFileNameWithoutExtension(Request.Files[i].FileName) + "_" + Guid.NewGuid().ToString("N") + Path.GetExtension(Request.Files[i].FileName); 
      string savePath = Server.MapPath("~/media/mm-signup-files/") + fileNamePath; 
      Request.Files[i].SaveAs(savePath); 
      Response.Write(fileNamePath + "|" + Path.GetFileName(Request.Files[i].FileName)); 

     } 
    } 
} 
1

可以在控制器的動作訪問請求對象,你也可以訪問請求對象在View中,它始終可以訪問。