我有一个网站已经运行了将近 6 个月。网站上有一个部分,用户最多可以上传 5 个属性图像。对于每 10 个用户,我有一个用户会遇到 System.NotSupportedException 问题。由于并非所有用户都发生这种情况,而且我似乎无法重现此错误,因此我不确定如何找到解决方案。查看错误消息,我认为这可能与他们机器上的网络配置有关,但我不能 100% 确定。任何想法/建议将不胜感激。
这是上传图像的代码。
private void UploadPhotos()
{
Stream objStream;
HttpFileCollection uploads = HttpContext.Current.Request.Files;
for (int i = 0; i < uploads.Count; i++)
{
if (uploads[i].ContentLength == 0)
continue;
try
{
string fileName = Path.Combine(image + "-" + i.ToString(), uploads[i].FileName);
//uploads[i].SaveAs(Server.MapPath("~/ExtendedImages/") + fileName);
using (new Impersonator("serverusername", @"serverlocation", "serverpassword!"))
{
try
{
uploads[i].SaveAs(@"serverlocation" + fileName);
}
catch (NotSupportedException ex)
{
uploads[i].SaveAs(Server.MapPath("~/ExtendedImages/") + fileName);
}
}
//uploads[i].SaveAs(Server.MapPath("~/ExtendedImages/") + fileName);
byte[] myimage = new byte[uploads[i].ContentLength];
objStream = uploads[i].InputStream;
objStream.Read(myimage, 0, uploads[i].ContentLength);
System.Drawing.Image Image1;
System.Drawing.Image Thumbnail;
Image1 = new Bitmap(objStream);
Image1.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
Image1.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
int NewWidth = 380;
int MaxHeight = 400;
if (Image1.Width <= NewWidth)
{
NewWidth = Image1.Width;
}
int NewHeight = Image1.Height * NewWidth / Image1.Width;
if (NewHeight > MaxHeight)
{
NewWidth = Image1.Width * MaxHeight / Image1.Height;
NewHeight = MaxHeight;
}
System.Drawing.Image NewImage = Image1.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
Thumbnail = Image1.GetThumbnailImage(100, 80, null, IntPtr.Zero);
MemoryStream ms = new MemoryStream();
MemoryStream ms2 = new MemoryStream();
Thumbnail.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
NewImage.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);
Photos photos = new Photos();
photos.ID = HomeTourSession.HometourName;
photos.AdId = (int)Session["adid"];
photos.FullPhoto = ms2.ToArray();
photos.ThumbnailPhoto = ms.ToArray();
photos.AlternateText = "";
photos.Insert();
}
catch (Exception exp)
{
throw new Exception("Error occured: " + exp);
}
}
}
这是一些用户遇到的错误。
发生错误:System.NotSupportedException:不支持给定路径的格式。在 System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath) 在 System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath) 在 System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess 访问, System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess 访问,AccessControlActions 控件,String[] pathList,布尔 checkForDuplicates,布尔 needFullPath)在 System.IO .FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share,