A有一个图像上传功能,可以在localhost上正常工作但是当我尝试在Windows Server 2003下运行时,我收到错误信息
这是代码..
现在在任何人跳过我之前;)我已经查看了之前的答案,我已经检查了所有权限,它们似乎是正确的..文件夹/路径存在等等。
ImageService imageService = new ImageService();
if (fileBase != null && fileBase.ContentLength > 0 && fileBase.ContentLength <= 2097152 && fileBase.ContentType.Contains("image/"))
{
var uploadedPath = "~/Assets/Images/";
Path.GetExtension(fileBase.ContentType);
var extension = Path.GetExtension(fileBase.FileName);
if (extension.ToLower() != ".jpg" && extension.ToLower() != ".gif") // only allow these types
{
photoViewModel.ImageValid = "Not Valid";
ModelState.AddModelError("Photo", "Wrong Image Type");
return View(photoViewModel);
}
EncoderParameters encodingParameters = new EncoderParameters(1);
encodingParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
ImageCodecInfo jpgEncoder = imageService.GetEncoderInfo("image/jpeg");
var uploadedimage = Image.FromStream(fileBase.InputStream, true, true);
Bitmap originalImage = new Bitmap(uploadedimage);
Bitmap newImage = new Bitmap(originalImage, 274, 354);
Graphics g = Graphics.FromImage(newImage);
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
var streamLarge = new MemoryStream();
newImage.Save(streamLarge, jpgEncoder, encodingParameters);
var fileExtension = Path.GetExtension(extension);
string newname;
if (photoViewModel.photoURL != null)
{ newname = photoViewModel.photoURL; }
else
{ newname = Guid.NewGuid() + fileExtension; }
var ImageName = newname;
newImage.Save(Server.MapPath(uploadedPath) + ImageName);
System.IO.File.WriteAllBytes(Server.MapPath(uploadedPath) + ImageName, streamLarge.ToArray());
photoViewModel.uploadedPath = uploadedPath;
photoViewModel.photoURL = ImageName;
originalImage.Dispose();
newImage.Dispose();
streamLarge.Dispose();
return View(photoViewModel);
}
答案 0 :(得分:3)
使用Image.FromStream
,流必须在图像的生命周期内保持打开状态。这意味着如果流是文件,文件将保持打开状态。我认为您需要处理uploadImage以允许关闭流(如果您等待GC清理uploadImage,这将在未来的不确定点发生 - 或者根本不会发生)。