asp.net2.0 我就是把图片上传到 photo 文件夹
代码:
try
{
if (FileUpload1.PostedFile.FileName == " ")
{
this.lb_info.Text = "请选择文件! ";
}
else
{
string filepath = FileUpload1.PostedFile.FileName;
string filename = filepath.Substring(filepath.LastIndexOf( "\\ ") + 1);
//string serverpath1 = Server.MapPath( "images/ ") + filename;
string serverpath2 = Server.MapPath( "images/ ") + System.DateTime.Now.ToString( "yyy-MM-dd-hh-mm-ss ") + Session.SessionID + filename;
// FileUpload1.PostedFile.SaveAs(serverpath1);
Response.Write(filepath);
ImageThumbnail img = new ImageThumbnail(filepath);
img.ReducedImage(200, 200, serverpath2);//0.4表示缩小40%
this.lb_info.Text = "上传成功! ";
}
}
catch (Exception error)
{
this.lb_info.Text = "上传发生错误!原因: " + error.ToString();
}
类代码:
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging; 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.FileNotFoundException: C:\Documents and Settings\Administrator\桌面\猫\13.jpg 在 System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) 在 System.Drawing.Image.FromFile(String filename) 在 ImageThumbnail..ctor(String ImageFileName) 位置 e:\wwwroot\App_Code\ImageThumbnail.cs:行号 15 在 admin_cus_manage_invete_edit.Button4_Click(Object sender, EventArgs e) 位置 e:\wwwroot\admin\cus_manage\invete_edit.aspx.cs:行号 197
问高手怎么该~~~
代码:
try
{
if (FileUpload1.PostedFile.FileName == " ")
{
this.lb_info.Text = "请选择文件! ";
}
else
{
string filepath = FileUpload1.PostedFile.FileName;
string filename = filepath.Substring(filepath.LastIndexOf( "\\ ") + 1);
//string serverpath1 = Server.MapPath( "images/ ") + filename;
string serverpath2 = Server.MapPath( "images/ ") + System.DateTime.Now.ToString( "yyy-MM-dd-hh-mm-ss ") + Session.SessionID + filename;
// FileUpload1.PostedFile.SaveAs(serverpath1);
Response.Write(filepath);
ImageThumbnail img = new ImageThumbnail(filepath);
img.ReducedImage(200, 200, serverpath2);//0.4表示缩小40%
this.lb_info.Text = "上传成功! ";
}
}
catch (Exception error)
{
this.lb_info.Text = "上传发生错误!原因: " + error.ToString();
}
类代码:
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging; 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.FileNotFoundException: C:\Documents and Settings\Administrator\桌面\猫\13.jpg 在 System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) 在 System.Drawing.Image.FromFile(String filename) 在 ImageThumbnail..ctor(String ImageFileName) 位置 e:\wwwroot\App_Code\ImageThumbnail.cs:行号 15 在 admin_cus_manage_invete_edit.Button4_Click(Object sender, EventArgs e) 位置 e:\wwwroot\admin\cus_manage\invete_edit.aspx.cs:行号 197
问高手怎么该~~~
解决方案 »
- 是高手请进来看看,棘手问题!!!
- <img src="http://.../xx.gif">跪求一正则表达式
- 在visual studio 2003 下新建不了web项目,提示:指定服务器运行的不是ASP.NET1.1版本
- 请教:使用扩展基类时提示基类的ConnectiongString属性未初始化
- 关于datagrid上添加radiobutton摸版列的问题。
- 欢迎 MCSD.NET MCAD.NET 的朋友来指教一下,来者有分!
- 树的兼容问题?
- 我想点击树某个节点就激发一个事件,如何做???
- 如何控制datagrid里编辑功能出现的TextBox的宽度?
- 多网站多域名绑定,一个网站能正常访问,一个报400错误
- 大家帮我看代码有什么问题,DataGird更新不到新数据!谢谢
- 如何理解“把一个对象存储于session中”,请对.NET基础了解深刻的同仁来探讨!
HttpContext.Current.Server.MapPath(@"photo\你的图片名")
未找到文件
路径错了 核对下吧
你可以先Response出你的路径看看是否正确再来做。
在C#里用String serverpath1 = Server.MapPath("~/photo/") + filename;
{
if (FileUpload1.PostedFile.FileName == " ")
{
this.lb_info.Text = "请选择文件! ";
}
else
{
string filepath = FileUpload1.PostedFile.FileName;
//string filename = filepath.Substring(filepath.LastIndexOf( "\\ ") + 1);
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
//string serverpath1 = Server.MapPath( "images/ ") + filename;
string serverpath2 = Server.MapPath("images/ ") + System.DateTime.Now.ToString("yyy-MM-dd-hh-mm-ss ") + Session.SessionID + filename;
FileUpload1.PostedFile.SaveAs(serverpath2);
Response.Write(filepath);
ImageThumbnail img = new ImageThumbnail(filepath);
img.ReducedImage(200, 200, serverpath2);//0.4表示缩小40%
this.lb_info.Text = "上传成功! ";
}
}
catch (Exception error)
{
this.lb_info.Text = "上传发生错误!原因: " + error.ToString();
}
===========================
楼主的这个地方好像多了一个空格,造成获取文件文件名字是错误。楼主可以看一下是不是这个问题。