2015-01-03 108 views
0

我正在使用Ninject進行依賴注入,並且我試圖將上傳的文件綁定到要調整圖像和其他內容的圖形處理程序。ASP.NET MVC 4狀態的上傳文件

我也有一個模型,有一個HttpPostedFileBase字段的上傳文件。問題是,如何在上傳文件到達控制器之前使用上傳的文件?

我試圖在這樣的Global.asax Application_BeginRequest()方法...

HttpContext.Current.Request.Files["UploadedFile"] 

但是這個代碼在Application_BeginRequest()返回null,但在模型中的控制器內,上傳的文件是存在的。

我現在在IIS中有禁止訪問global.asax中的HttpContext的mod,但是有沒有可以使用的解決方法或其他global.asax方法?

`

回答

0

您可以創建自定義ActionFilter指定的操作方法之前訪問發佈文件:

public class ImageModifierAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     var files = filterContext.HttpContext.Request.Files; 
     foreach (string file in files) 
     { 
      var postedFile = files[file]; 
      if (postedFile == null || postedFile.ContentLength == 0) continue; 
      // modify the postedFile Here 
     } 
     base.OnActionExecuting(filterContext); 
    } 
} 

然後使用它:

[ImageModifier] 
public ActionResult ImageUpload(HttpPostedFileBase file) 
{ /*.....*/ } 
+0

謝謝你的答案,但是我需要上傳的文件才能到達控制器,因爲Ninject是在global.asax中啓動的。我有一個GraphicsHandler對象需要HttpPostedFileBase的依賴,所以我真的不能等待控制器 –