Download the file and send it to the service level, i.e. C # class library

I am trying to upload a file and send it to the service level for saving, however I continue to find examples of how the controller receives the HTTPPostedFileBase and stores it directly in the controller. My service level has no limitations in web dll, so I need to read my object in a memory stream / byte? Any pointers on how I should do this are greatly appreciated ...

Note. Files can be written in pdf format, so I may also need to check the type of content (possibly within the domain level of the domain ...

the code:

   public ActionResult UploadFile(string filename, HttpPostedFileBase thefile)
{
//what do I do here...?


}

EDIT:

public interface ISomethingService    
{
  void AddFileToDisk(string loggedonuserid, int fileid, UploadedFile newupload);    
}
    public class UploadedFile
    {
        public string Filename { get; set; }
        public Stream TheFile { get; set; }
        public string ContentType { get; set; }
    }

public class SomethingService : ISomethingService    
{
  public AddFileToDisk(string loggedonuserid, int fileid, UploadedFile newupload)
  {
    var path = @"c:\somewhere";
    //if image
     Image _image = Image.FromStream(file);
     _image.Save(path);
    //not sure how to save files as this is something I am trying to find out...
  } 
}
+5
source share
2 answers

InputStream , ContentType FileName, :

public ActionResult UploadFile(string filename, HttpPostedFileBase thefile)
{
    if (thefile != null && thefile.ContentLength > 0)
    {
        byte[] buffer = new byte[thefile.ContentLength];
        thefile.InputStream.Read(buffer, 0, buffer.Length);
        _service.SomeMethod(buffer, thefile.ContentType, thefile.FileName);
    }
    ...
}
+10

, Stream , theFile.InputStream? - , , , .

+1

All Articles