I am working on an ASP.NET MVC 4
application and there are many views where the user can upload a file which I save on the server. Along with that I have a separate entity that holds different data for the uploaded file like :
string fileExtension = System.IO.Path.GetExtension(uploadFile.FileName);
string uniqueGuid = Helper.GetUniqueName(Server.MapPath("~/Content/Files"));
string newFileName = uniqueGuid + fileExtension;
this is just to get the impression. There is a number of operations that I do for each file uploaded from the user and after I'm done I make new record in the database with the info and save the file on the server.
My Solution has two projects - one is the MVC 4
application, and the other is used as data access layer. Where using Entity Framework 5
I've implemented Repository pattern
and UnitOfWork
.
what I want to do is to create new method in my repository. Something like:
public bool ManageFile(HttpPostedFileBase file );
and call this method from my controllers instead writing the same logic the same time.
A standard Action
in my controller that accepts HttpPostedFileBase file
looks like:
public ActionResult Edit(SomeViewModel model, HttpPostedFileBase uploadFile)
inside this action I want to call:
unitOfWork.SomeModelRepository.ManageFile(uploadFile);
the problem is that my data access layer project doesn't recognize the HttpPostedFileBase
. Maybe I can add some reference to the project, I'm not sure since I've had some problem using Server.MapPath
for example outside the Action
but even if I can reference it I'm still not sure if it's the better approach. After all my data access layer is only this - a layer of abstraction I don't think that HttpPostedFileBase
has a place there.
In my controller Action
I've tried to parse HttpPostedFileBase uploadFile
to File uploadFile
but I couldn't do that. But basically what I need is the ability to perform the same operations that one could perform on file - get extension, get name and so on... So how can pass HttpPostedFileBase uploadFile
to my DAL
project?