【问题标题】:Getting 'Stream does not support writing.' Exception in the following code获取“流不支持写入”。以下代码中的异常
【发布时间】:2011-08-30 02:14:24
【问题描述】:

我正在尝试将图像上传到 Amazon S3,但在此之前我正在调整图像大小。为了调整大小,我必须传递流对象,并且在某一点(注释为 //Error 的行)我得到“流不支持写入”。例外。请帮忙。

 public ActionResult AddPost(AddPost post)
    {
        Guid guid = new Guid();
        AccountController ac=new AccountController();

        string randomId = guid.ToString();

        PutAttributesRequest putAttributesAction = new PutAttributesRequest().WithDomainName("ThisIsMyEXDomainPosts").WithItemName(randomId);

        List<ReplaceableAttribute> attrib = putAttributesAction.Attribute;

        System.IO.Stream stream;

        System.IO.StreamReader sr = new System.IO.StreamReader(post.imageFileAddress.ToString());


        sr.ReadToEnd();

        stream = sr.BaseStream;

        Amazon.S3.Model.PutObjectRequest putObjectRequest = new Amazon.S3.Model.PutObjectRequest();

        System.Drawing.Image img = System.Drawing.Image.FromStream(stream);

        System.Drawing.Image imgResized = ResizeImage(img, 640, 800);

        System.IO.MemoryStream mstream = new System.IO.MemoryStream();

        imgResized.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg);

        mstream.WriteTo(stream);//Error

        putObjectRequest.WithBucketName("TIMEXImages");
        putObjectRequest.CannedACL = Amazon.S3.Model.S3CannedACL.PublicRead;
        putObjectRequest.Key = randomId + "_0.jpg";
        putObjectRequest.InputStream = stream;

        Amazon.S3.Model.S3Response s3Response = as3c.PutObject(putObjectRequest);
        s3Response.Dispose();

        //Uploadig the Thumb

        System.Drawing.Image imgThumb = ResizeImage(img, 80, 100);

        imgThumb.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg);

        mstream.WriteTo(stream);

        putObjectRequest.WithBucketName("MyProjectImages");
        putObjectRequest.CannedACL = Amazon.S3.Model.S3CannedACL.PublicRead;
        putObjectRequest.Key = randomId + ".jpg";
        putObjectRequest.InputStream = stream;

        Amazon.S3.Model.S3Response s3Response2 = as3c.PutObject(putObjectRequest);
        s3Response2.Dispose();


        //Closing all opened streams
        sr.Close();

        stream.Close();

        mstream.Close();



        //Adding to SimpleDB
        attrib.Add(new ReplaceableAttribute().WithName("category").WithValue(post.category));
        attrib.Add(new ReplaceableAttribute().WithName("description").WithValue(post.description));
        attrib.Add(new ReplaceableAttribute().WithName("favoriteCount").WithValue("0"));
        attrib.Add(new ReplaceableAttribute().WithName("imageThug").WithValue(randomId));
        attrib.Add(new ReplaceableAttribute().WithName("title").WithValue(post.title));
        attrib.Add(new ReplaceableAttribute().WithName("userId").WithValue(ac.GetLoggedInUserId()));

        sdb.PutAttributes(putAttributesAction);




        return View();
    }

【问题讨论】:

    标签: c# stream .net


    【解决方案1】:

    似乎StreamReaderBaseStream 只是读取 - 这是有道理的。为什么你首先需要重用这个流呢?直接使用内存流即可:

    mstream.Position = 0;
    putObjectRequest.InputStream = mstream;
    

    【讨论】:

    • 我不是编码专家,但据我所知,我这样做是因为“putObjectRequest.InputStream”采用“System.IO.Stream 流”类型的对象。
    • @Umair: MemoryStreamStream
    【解决方案2】:

    输入流不支持写入,反之亦然输出流。看看你是否互换了它的意思。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      • 2014-10-13
      • 2012-09-14
      • 2014-09-12
      • 1970-01-01
      • 2022-11-30
      • 2022-12-28
      相关资源
      最近更新 更多