在网上弄了个上传图片自动生成缩略图和按日期命名的程序,现在在本机调试都正常,就是上传到服务器后一上传图片就出错。请问是哪里设置有问题呢?
错误代码:System.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\My Documents\My Pictures\新建文件夹 (2)\3.jpg'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) at OilDepotOilcanadd.IsAllowedExtension(FileUpload hifile) in c:\Inetpub\wwwroot\OilDepotoildischargetrestleadd.aspx.cs:line 100 at OilDepotOilcanadd.Btn_sctp_Click(Object sender, EventArgs e) in c:\Inetpub\wwwroot\OilDepotoildischargetrestleadd.aspx.cs:line 56 protected void Btn_sctp_Click(object sender, EventArgs e)
{
try
{
//判断是否已经选取文件
if (FileUpload1.HasFile)
{
if (IsAllowedExtension(FileUpload1))
{
//取得文件的扩展名,并转换成小写
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower(); string path1 = Server.MapPath("~/Bigimages/");
string path2 = Server.MapPath("~/Smallimages/");
string fileName = DateRndName() + fileExtension;
FileUpload1.PostedFile.SaveAs(path1 + fileName);
string bigpath = Server.MapPath("~/Bigimages/" + fileName);
//生成缩略图并存进文件夹
ImageThumbnail img = new ImageThumbnail(FileUpload1.PostedFile.FileName); img.ReducedImage(130, 100, path2 + fileName);//200,200表是长和宽都为200
//img.ReducedImage(0.4, path2 + fileName);//0.4表示缩小40%
//File.Delete(bigpath);
Response.Write("<script>alert(\"上传成功\");</script>");
this.Image1.ImageUrl = "~/Smallimages/" + fileName;
Lab_dtlj.Text = "~/Bigimages/" + fileName;
Lab_tpdz.Text = this.Image1.ImageUrl;
}
else
{
Response.Write("<script>alert(\"您只能上传jpg或者gif图片\");</script>");
} }
else
{
Response.Write("<script>alert(\"你还没有选择文件\");</script>");
}
}
catch (Exception error)
{
Response.Write(error.ToString());
}
}
//真正判断文件类型的关键函数
public static bool IsAllowedExtension(FileUpload hifile)
{
System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString(); }
catch
{ }
r.Close();
fs.Close();
if (fileclass == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
{
return true;
}
else
{
return false;
} } #region DateRndName()日期时间+3位数随机
string DateRndName()
{
Random ra = new Random();
DateTime d = DateTime.Now;
string s = null, y, m, dd, h, mm, ss;
y = d.Year.ToString();
m = d.Month.ToString();
if (m.Length < 2) m = "0" + m;
dd = d.Day.ToString();
if (dd.Length < 2) dd = "0" + dd;
h = d.Hour.ToString();
if (h.Length < 2) h = "0" + h;
mm = d.Minute.ToString();
if (mm.Length < 2) mm = "0" + mm;
ss = d.Second.ToString();
if (ss.Length < 2) ss = "0" + ss;
s += y + m + dd + h + mm + ss;
s += ra.Next(100, 999).ToString();
return s;
}
#endregion
public class ImageThumbnail
{
public Image ResourceImage;
private int ImageWidth;
private int ImageHeight;
public string ErrorMessage; public ImageThumbnail(string ImageFileName)
{
ResourceImage = Image.FromFile(ImageFileName);
ErrorMessage = "";
} public bool ThumbnailCallback()
{
return false;
}
// 方法1,按大小
public bool ReducedImage(int Width, int Height, string targetFilePath)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
ReducedImage.Dispose();
return true;
}
catch (Exception e)
{
ErrorMessage = e.Message;
return false;
}
}
// 方法2,按百分比 缩小60% Percent为0.6 targetFilePath为目标路径
public bool ReducedImage(double Percent, string targetFilePath)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);
ImageHeight = (ResourceImage.Height) * ImageWidth / ResourceImage.Width;//等比例缩放
ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
ReducedImage.Dispose();
return true;
}
catch (Exception e)
{
ErrorMessage = e.Message;
return false;
}
}
}
错误代码:System.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\My Documents\My Pictures\新建文件夹 (2)\3.jpg'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) at OilDepotOilcanadd.IsAllowedExtension(FileUpload hifile) in c:\Inetpub\wwwroot\OilDepotoildischargetrestleadd.aspx.cs:line 100 at OilDepotOilcanadd.Btn_sctp_Click(Object sender, EventArgs e) in c:\Inetpub\wwwroot\OilDepotoildischargetrestleadd.aspx.cs:line 56 protected void Btn_sctp_Click(object sender, EventArgs e)
{
try
{
//判断是否已经选取文件
if (FileUpload1.HasFile)
{
if (IsAllowedExtension(FileUpload1))
{
//取得文件的扩展名,并转换成小写
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower(); string path1 = Server.MapPath("~/Bigimages/");
string path2 = Server.MapPath("~/Smallimages/");
string fileName = DateRndName() + fileExtension;
FileUpload1.PostedFile.SaveAs(path1 + fileName);
string bigpath = Server.MapPath("~/Bigimages/" + fileName);
//生成缩略图并存进文件夹
ImageThumbnail img = new ImageThumbnail(FileUpload1.PostedFile.FileName); img.ReducedImage(130, 100, path2 + fileName);//200,200表是长和宽都为200
//img.ReducedImage(0.4, path2 + fileName);//0.4表示缩小40%
//File.Delete(bigpath);
Response.Write("<script>alert(\"上传成功\");</script>");
this.Image1.ImageUrl = "~/Smallimages/" + fileName;
Lab_dtlj.Text = "~/Bigimages/" + fileName;
Lab_tpdz.Text = this.Image1.ImageUrl;
}
else
{
Response.Write("<script>alert(\"您只能上传jpg或者gif图片\");</script>");
} }
else
{
Response.Write("<script>alert(\"你还没有选择文件\");</script>");
}
}
catch (Exception error)
{
Response.Write(error.ToString());
}
}
//真正判断文件类型的关键函数
public static bool IsAllowedExtension(FileUpload hifile)
{
System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString(); }
catch
{ }
r.Close();
fs.Close();
if (fileclass == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
{
return true;
}
else
{
return false;
} } #region DateRndName()日期时间+3位数随机
string DateRndName()
{
Random ra = new Random();
DateTime d = DateTime.Now;
string s = null, y, m, dd, h, mm, ss;
y = d.Year.ToString();
m = d.Month.ToString();
if (m.Length < 2) m = "0" + m;
dd = d.Day.ToString();
if (dd.Length < 2) dd = "0" + dd;
h = d.Hour.ToString();
if (h.Length < 2) h = "0" + h;
mm = d.Minute.ToString();
if (mm.Length < 2) mm = "0" + mm;
ss = d.Second.ToString();
if (ss.Length < 2) ss = "0" + ss;
s += y + m + dd + h + mm + ss;
s += ra.Next(100, 999).ToString();
return s;
}
#endregion
public class ImageThumbnail
{
public Image ResourceImage;
private int ImageWidth;
private int ImageHeight;
public string ErrorMessage; public ImageThumbnail(string ImageFileName)
{
ResourceImage = Image.FromFile(ImageFileName);
ErrorMessage = "";
} public bool ThumbnailCallback()
{
return false;
}
// 方法1,按大小
public bool ReducedImage(int Width, int Height, string targetFilePath)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
ReducedImage.Dispose();
return true;
}
catch (Exception e)
{
ErrorMessage = e.Message;
return false;
}
}
// 方法2,按百分比 缩小60% Percent为0.6 targetFilePath为目标路径
public bool ReducedImage(double Percent, string targetFilePath)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);
ImageHeight = (ResourceImage.Height) * ImageWidth / ResourceImage.Width;//等比例缩放
ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
ReducedImage.Dispose();
return true;
}
catch (Exception e)
{
ErrorMessage = e.Message;
return false;
}
}
}
{
Random ra = new Random();
DateTime d = DateTime.Now;
string s = null, y, m, dd, h, mm, ss;
y = d.Year.ToString();
m = d.Month.ToString();
if (m.Length < 2) m = "0" + m;
dd = d.Day.ToString();
if (dd.Length < 2) dd = "0" + dd;
h = d.Hour.ToString();
if (h.Length < 2) h = "0" + h;
mm = d.Minute.ToString();
if (mm.Length < 2) mm = "0" + mm;
ss = d.Second.ToString();
if (ss.Length < 2) ss = "0" + ss;
s += y + m + dd + h + mm + ss;
s += ra.Next(100, 999).ToString();
return s;
}
非常寒这段代码肯定是Asp转过来的
1.在服务器上检查下面的路径是否存在,
D:\My Documents\My Pictures\新建文件夹 (2)\3.jpg
2.并给"新建文件夹 (2)"这个文件夹IIS_WPG的可写权限
我现在是在我机子上想通过程序上传到服务器上,但是就是提示出错!
D:\My Documents\My Pictures\新建文件夹 (2)\3.jpg
这个路径是我本机电脑上的,不是在服务器上的!
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();改成string fileExtension = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower();
private bool IsAllowedExtension(HttpPostedFile hifile)
{
bool ret = false;
int fileLen = hifile.ContentLength;
byte[] imgArray = new byte[fileLen]; System.IO.FileStream fs = new System.IO.FileStream(hifile.FileName, System.IO.FileMode.Open);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
}
catch
{
return false;
}
r.Close();
fs.Close();
/*文件扩展名说明
*7173 gif
*255216 jpg
*13780 png
*6677 bmp
*239187 txt,aspx,asp,sql
*208207 xls.doc.ppt
*6063 xml
*6033 htm,html
*4742 js
*8075 xlsx,zip,pptx,mmap,zip
*8297 rar
*01 accdb,mdb
*7790 exe,dll
*5666 psd
*255254 rdp
*10056 bt种子
*64101 bat
*/
String[] fileType = { "255216", "208207" }; for (int i = 0; i < fileType.Length; i++)
{
if (fileclass == fileType[i])
{
ret = true;
break;
}
}
return ret;
}
private bool IsAllowedExtension(FileUpload hifile)
{
bool ret = false; int fileLen = hifile.PostedFile.ContentLength;
byte[] imgArray = new byte[fileLen];
hifile.PostedFile.InputStream.Read(imgArray, 0, fileLen);
MemoryStream ms = new MemoryStream(imgArray);
System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
string fileclass = "";
byte buffer;
try
{
buffer = br.ReadByte();
fileclass = buffer.ToString();
buffer = br.ReadByte();
fileclass += buffer.ToString();
//buffer = br.ReadByte();
//fileclass += buffer.ToString();
}
catch
{
}
br.Close();
ms.Close();
/*文件扩展名说明
*7173 gif
*255216 jpg
*13780 png
*6677 bmp
*239187 txt,aspx,asp,sql
*208207 xls.doc.ppt
*6063 xml
*6033 htm,html
*4742 js
*8075 xlsx,zip,pptx,mmap,zip
*8297 rar
*01 accdb,mdb
*7790 exe,dll
*5666 psd
*255254 rdp
*10056 bt种子
*64101 bat
*/
String[] fileType = { "208207" }; for (int i = 0; i < fileType.Length; i++)
{
if (fileclass == fileType[i])
{
ret = true;
break;
}
}
return ret;
}
路径不对 无法读取