开发者

Getting 'Stream does not support writing.' Exception in the following code

开发者 https://www.devze.com 2023-03-31 11:15 出处:网络
I am trying to upload an image to Amazon S3 but before that I am resizing the image. For resizing I have to pass the stream objects and at one point (line commented as //Error) I am getting \'Stream d

I am trying to upload an image to Amazon S3 but before that I am resizing the image. For resizing I have to pass the stream objects and at one point (line commented as //Error) I am getting 'Stream does not support writing.' Exception. Please help.

 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;
       开发者_如何学JAVA 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();
    }


It seems like the BaseStream of a StreamReader is read only - which makes sense. Why do you need to re-use this stream in the first place though? Just use the memory stream directly:

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


Input stream doesnt support writing and vice versa is with Output stream. See if you have interchanged its meaning.

0

精彩评论

暂无评论...
验证码 换一张
取 消