关于FileUpload控件解决方法
关于FileUpload控件
我要上传图片到服务器,使用了这个控件,在上传时我需生成缩略图,大致代码如下:
if (FileUpload1.HasFile)
{
if (FileUpload1.PostedFile.ContentLength < 10240)
{
//获取文件路径(Postedfile对象提供对客户端已上载的单独文件的访问)。
string filepath =FileUpload1.PostedFile.FileName;
//获取不包含路径的文件名(包含文件格式)。
string filename = FileUpload1.FileName;
//获取文件的格式
string fileEx = filename.Substring(filename.LastIndexOf('.') + 1).ToUpper();
//获取商品图片服务器的物理路径
string serverpath = Server.MapPath(@"~\images\product\") + filename;
//保存到数据库的文件虚拟路径
string relativepath = @"~\images\product\" + filename;
if (fileEx == "JPG")
{
if(File.Exists(serverpath))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "repeat", "alert('此图片已存在,不能重复上传!')", true);
}
else
{
//生成缩略图
System.Drawing.Image picture,newpicture;
//从指定文件创建图片
picture = System.Drawing.Image.FromFile(filepath); System.Drawing.Image.GetThumbnailImageAbort callb = null;
newpicture = picture.GetThumbnailImage(175, 131, callb,new IntPtr());
//把缩略图保存到指定的路径。
newpicture.Save(serverpath);
//释放Image对象占用的资源。
picture.Dispose();
newpicture.Dispose();
。。。。。。。
现在的问题是FileUpload1.PostedFile.FileName以前得到的是完全的客户端路径,现在得到只是文件名,找过相关资料说是安全问题不能得到客户端的路径,但以前我做过项目也是怎么写的没有出错,到现在还运行的好好的,为什么现在这段代码就不行了,咋回事??蛋疼啊!!!!!!高手解答一下,如果我要达到目的怎么变通呢????
想给高分,可是没分了对不起了!!!,(小弟弟也疼了)
------解决方案--------------------
一般要先把图片传到服务器,然后再压缩,
picture = System.Drawing.Image.FromFile(filepath); System.Drawing.Image.GetThumbnailImageAbort callb = null;
这个filepath路径有问题
还有就是你没有语句把文件传到服务器,传上去了 获取服务器的路径代替filepath
我要上传图片到服务器,使用了这个控件,在上传时我需生成缩略图,大致代码如下:
if (FileUpload1.HasFile)
{
if (FileUpload1.PostedFile.ContentLength < 10240)
{
//获取文件路径(Postedfile对象提供对客户端已上载的单独文件的访问)。
string filepath =FileUpload1.PostedFile.FileName;
//获取不包含路径的文件名(包含文件格式)。
string filename = FileUpload1.FileName;
//获取文件的格式
string fileEx = filename.Substring(filename.LastIndexOf('.') + 1).ToUpper();
//获取商品图片服务器的物理路径
string serverpath = Server.MapPath(@"~\images\product\") + filename;
//保存到数据库的文件虚拟路径
string relativepath = @"~\images\product\" + filename;
if (fileEx == "JPG")
{
if(File.Exists(serverpath))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "repeat", "alert('此图片已存在,不能重复上传!')", true);
}
else
{
//生成缩略图
System.Drawing.Image picture,newpicture;
//从指定文件创建图片
picture = System.Drawing.Image.FromFile(filepath); System.Drawing.Image.GetThumbnailImageAbort callb = null;
newpicture = picture.GetThumbnailImage(175, 131, callb,new IntPtr());
//把缩略图保存到指定的路径。
newpicture.Save(serverpath);
//释放Image对象占用的资源。
picture.Dispose();
newpicture.Dispose();
。。。。。。。
现在的问题是FileUpload1.PostedFile.FileName以前得到的是完全的客户端路径,现在得到只是文件名,找过相关资料说是安全问题不能得到客户端的路径,但以前我做过项目也是怎么写的没有出错,到现在还运行的好好的,为什么现在这段代码就不行了,咋回事??蛋疼啊!!!!!!高手解答一下,如果我要达到目的怎么变通呢????
想给高分,可是没分了对不起了!!!,(小弟弟也疼了)
------解决方案--------------------
一般要先把图片传到服务器,然后再压缩,
picture = System.Drawing.Image.FromFile(filepath); System.Drawing.Image.GetThumbnailImageAbort callb = null;
这个filepath路径有问题
还有就是你没有语句把文件传到服务器,传上去了 获取服务器的路径代替filepath