加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

上传文件并发送到服务层,即c#类库

发布时间:2020-12-15 18:17:59 所属栏目:百科 来源:网络整理
导读:我正在尝试上传文件并将其发送到服务层进行保存,但是我一直在寻找控制器如何获取HTTPPostedFileBase并将其直接保存在控制器中的示例.我的服务层在web dll上没有依赖关系,因此我需要将我的对象读入内存流/字节吗?关于我应该怎么做的任何指示非常感谢… 注意
我正在尝试上传文件并将其发送到服务层进行保存,但是我一直在寻找控制器如何获取HTTPPostedFileBase并将其直接保存在控制器中的示例.我的服务层在web dll上没有依赖关系,因此我需要将我的对象读入内存流/字节吗?关于我应该怎么做的任何指示非常感谢…

注意:文件可以通过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,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...
  } 
}

解决方法

您可以使用已发布文件的 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,buffer.Length);
        _service.SomeMethod(buffer,thefile.ContentType,thefile.FileName);
    }
    ...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读