我的网站已经运行了近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)at System.Security.Util.StringExpressionSet.CreateListFromExpressions(字符串[] str,Boolean needFullPath)at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access,AccessControlActions控件,String [] pathListOrig,Boolean checkForDuplicates,Boolean needFullPath,Boolean copyPathList)at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access,AccessControlActions控件,String [] pathList,Boolean checkForDuplicates,Boolean needFullPath)at System.IO.FileStream.Init(String path,FileMode mode,FileAccess 访问,Int32权限,布尔useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,String System.IO.FileStream..ctor中的msgPath,Boolean bFromProxy(String path,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项,String msgPath,Boolean bFromProxy) 在System.IO.FileStream..ctor(String path,FileMode mode)at
处的System.Web.HttpPostedFile.SaveAs(String filename)