开发者

mvc - file upload in external class

开发者 https://www.devze.com 2022-12-16 22:46 出处:网络
Im using scott hanselmans file upload code: public ActionResult UploadFiles() { var r = new List(); foreach (string file in Request.Files)

Im using scott hanselmans file upload code:

public ActionResult UploadFiles()

{ var r = new List();

  foreach (string file in Request.Files)
  {
     HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
     if (hpf.ContentLength == 0)
        continue;
     string savedFileName = Path.Combine(
        AppDomain.CurrentDomain.BaseDirectory, 
        Path.GetFileName(hpf.FileName));
     hpf.SaveAs(savedFileName);

     r.Add(new ViewDataUploadFilesResult() 
        { Name = savedFileName, 
          Length = hpf.ContentLength });
  }
  return View("UploadedFiles",r);
开发者_如何学编程

}

I dont want this to exist in the controller. rather call it as a static method in a utils.cs class

is this possible?


Yes, but you'll need to pass your request object in to the function, as an outside library won't have access to it.

public void UploadFile(HttpRequestBase request) { ... }
0

精彩评论

暂无评论...
验证码 换一张
取 消