2011-02-14 46 views
5

我想上传一个文件,并将其发送到服务层保存,但我不断找到有关如何控制器获取HTTPPostedFileBase并将其直接保存在控制器。我的服务层对web dll没有依赖性,因此我是否需要将我的对象读入内存流/字节?我应该如何去这个任何指针是极大的赞赏...上传文件并发送到服务层即ie#c#类库

注:文件可以通过PDF,Word,以便我可能还需要检查的内容类型(可能内域名服务层...

代码:

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


} 

编辑:

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... 
    } 
} 
+0

你能告诉我们你的服务层是怎样的吗? – 2011-02-14 11:27:08

回答

10

您可以使用贴FIL的InputStream财产E要阅读的内容作为字节数组,并将其发送给服务层与其他信息,如ContentTypeFileName您的服务层可能需要沿着:

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); 
    } 
    ... 
} 
1

你能不能在业务层上创建的方法接受Stream作为参数并将theFile.InputStream传递给它?流不需要任何与Web相关的依赖关系,并且避免通过复制其他数据结构中的数据来消耗内存来复制内存。

相关问题