tags:

views:

70

answers:

1

Hi,

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?

+1  A: 

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) { ... }
Russell Steen
Its a static method in the library, its giving this as the error:Cannot convert type 'System.Web.HttpPostedFile' to 'System.Web.HttpPostedFileBase' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
raklos
The problem there is the cast, not the function rewrite asHttpPostedFileBase hpf = (HttpPostedFileBase)request.Files[file];
Russell Steen