注册 登录
编程论坛 ASP.NET技术论坛

上传图片到文件夹时出错

sss333 发布于 2007-05-16 16:57, 718 次点击

protected void Button1_Click(object sender, EventArgs e)
{
string ext = null, path = null, newFileName = null;
System.Drawing.Image image, newImage;
if (FileUpload1.PostedFile.FileName != null && FileUpload1.PostedFile.FileName != "")
{

//载入原图
image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
//回调
System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(callBack);
//生成缩略图
newImage = image.GetThumbnailImage(image.Width / 3, image.Height / 3, callb, new System.IntPtr());
image.Dispose();
newImage.Dispose();

if (FileUpload1.PostedFile != null)
{
//判断是不是图像文件
if (FileUpload1.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
{
Response.Write("文件类型错误");//文件类型错误
}
else
{
ext = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("."));
//生成新的原文件名 年月日+文件大小+扩展名
path = "upload/" + System.DateTime.Now.Date.ToShortDateString() + FileUpload1.PostedFile.ContentLength.ToString() + ext;

//缩略图文件名
newFileName = "upload/" + System.DateTime.Now.Date.ToShortDateString() + FileUpload1.PostedFile.ContentLength.ToString() + "small" + ext;
//上传缩略图
Bitmap output = new Bitmap(newImage);
//Graphics g = Graphics.FromImage(output);
//output.Save(Server.MapPath(newFileName));
output.save(newFileName);

//FileUpload1.PostedFile.SaveAs(Server.MapPath(newFileName));
FileUpload1.PostedFile.SaveAs(Server.MapPath(path));
}
}
}
else
{
path = null;
newFileName = null;

}
}
标记的那句话有错,应该怎么改呀?大家帮帮忙!谢谢!

5 回复
#2
ahuinan2007-05-16 19:39

自己参考下面的代码,比你好,最起码在判断文件类型部分比你先进.

[URL=http://www.sosuo8.com/article/show.asp?id=1019]http://www.sosuo8.com/article/show.asp?id=1019[/URL]
#3
tel19822007-05-16 22:35
不是太清楚,学习中…………
#4
rainic2007-05-17 08:04
以下是引用ahuinan在2007-5-16 19:39:50的发言:

自己参考下面的代码,比你好,最起码在判断文件类型部分比你先进.

[URL=http://www.sosuo8.com/article/show.asp?id=1019]http://www.sosuo8.com/article/show.asp?id=1019[/URL]

可能是准确了,不过慢了好多...

没必要那样做

[此贴子已经被作者于2007-5-17 8:04:41编辑过]

#5
无聊的爱2007-05-17 11:24
shang chuan wen jian yi lei dai ma henshao xie
#6
Kendy1234562007-05-17 11:41

你这哪里是上传文件出错 你是保存图片的时候出错

1