在以下代码中出现“流不支持写入”异常

5

我正在尝试将图像上传到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();
    }
2个回答

1

看起来StreamReaderBaseStream是只读的 - 这很有道理。但你为什么需要重新使用这个流呢?直接使用内存流就可以了:

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

我不是专业的编码人员,但凭借我的最佳知识,我正在这样做,因为'putObjectRequest.InputStream'需要一个'System.IO.Stream stream'类型的对象。 - Umair Khan Jadoon
1
@Umair:MemoryStream 是一个 Stream - BrokenGlass

1

输入流不支持写入,输出流也是如此。请检查是否交换了它们的含义。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接