【问题标题】:Convert MemoryStream to byte[]- Cannot access a closed Stream将 MemoryStream 转换为 byte[]- 无法访问已关闭的 Stream
【发布时间】:2013-07-15 17:21:53
【问题描述】:

我有一个文件上传可以异步上传文件

   $("#files").kendoUpload({
        async: {
            saveUrl: "AddFile",              
              autoUpload: true
          }
    });

在 AddFile 操作中,我将发布的文件存储在 tempData 中

   public ActionResult AddFile(IEnumerable<HttpPostedFileBase> files)
    {
        TempData["PostedFile"] = files;
        // Return an empty string to signify success
        return Content("");
    }

在创建操作中,当我希望将张贴文件 Inputdtream 转换为 byte[] 时出现异常

无法访问已关闭的文件

  if ((TempData["PostedFile"] as IEnumerable<HttpPostedFileBase>)!= null)
     {

      var postedfile =(TempData["PostedFile"] as IEnumerable<HttpPostedFileBase>).ElementAt(0);

       MemoryStream target = new MemoryStream();
       postedfile.InputStream.CopyTo(target);//This Line Has exception
       byte[] data = target.ToArray();
      }

更新 当我上传一个大小为 80Kb 的文件时,我的代码可以工作......当我的文件大小为 500k 时,我的代码有异常!!!!!!!!!

【问题讨论】:

    标签: c# asp.net-mvc stream


    【解决方案1】:

    这是 MVC 引擎的正确行为。为什么不在 AddFile 操作中将字节数组存储在 TempData 中?

    【讨论】:

    • 检查我的更新问题
    猜你喜欢
    • 2011-12-27
    • 2012-06-11
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 2020-03-09
    相关资源
    最近更新 更多