获取“Stream 不支持写入。”以下代码中出现异常
我正在尝试将图像上传到 Amazon S3,但在此之前我正在调整图像大小。为了调整大小,我必须传递流对象,并且在某一时刻(注释为 //Error 的行)我收到“Stream 不支持写入”。例外。请帮忙。
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();
}
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;
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来
StreamReader
的BaseStream
是只读的 - 这是有道理的。为什么你首先需要重新使用这个流呢?直接使用内存流即可:It seems like the
BaseStream
of aStreamReader
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:输入流不支持写入,输出流反之亦然。看看你是否互换了它的意思。
Input stream doesnt support writing and vice versa is with Output stream. See if you have interchanged its meaning.