您的位置:程序门 -> .net技术 -> asp.net



用ajax上传图片,在本地测试可以,上传服务器不行,请指点


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


用ajax上传图片,在本地测试可以,上传服务器不行,请指点[已结贴,结贴人:freeboy827]
发表于:2007-07-10 17:14:17 楼主
代码
system.drawing.image   img   =   system.drawing.image.fromfile(fileinfo);
system.io.memorystream   ms   =   new   system.io.memorystream();
//取得文件的扩展名,保存文件格式
int   j=fileinfo.lastindexof( "\\ ");
int   i=fileinfo.lastindexof( ". ");
string   filename=fileinfo.substring(j+1,i-j);
string   extname=fileinfo.substring(i+1);
switch(extname.toupper())
{
case   "gif ":
img.save(ms,   system.drawing.imaging.imageformat.gif);
break;
default   :
img.save(ms,system.drawing.imaging.imageformat.jpeg);
break;
}
string   newname=datetime.now.year.tostring()+datetime.now.month.tostring()+datetime.now.day.tostring()+datetime.now.hour.tostring()+datetime.now.minute.tostring()+datetime.now.second.tostring();
byte[]   by   =   ms.toarray();
string   savepath   =   server.mappath( "images\\product ");
filestream   filestream   =   null;
try
{
filestream   =   new   filestream(savepath.replace( "\\ajax ", " ")   +   "c:\\ "   +   newname+ ". "+extname,   filemode.create,   fileaccess.write);
//filestream   =   new   filestream(   "c:\\up\\ "   +   newname+ ". "+extname,   filemode.create,   fileaccess.write);
//write   the   file
filestream.write(by,   0,   by.length);
filestream.flush();//clear   the   buffer,write   the   data   to   the   hard   disk

}
catch(exception   ex)
{
string   s=ex.tostring();
throw;
}
finally
{
filestream.close();
}
发表于:2007-07-10 17:19:041楼 得分:0
估计是权限问题。看看放上传图片的文件夹的写权限给了没。把报错信息贴上来看看。
发表于:2007-07-10 17:28:232楼 得分:0
看看异常了
发表于:2007-07-10 17:32:273楼 得分:0
没有反应,
发表于:2007-07-10 17:34:024楼 得分:0
先确定权限给了没
发表于:2007-07-10 17:45:555楼 得分:0
权限是有的,刚开始不是ajax实现的,在服务器可以上传图片,改成ajax后在本地测试是可以的,上传到服务器就不行了
发表于:2007-07-10 17:46:386楼 得分:0
路径问题
发表于:2007-07-10 18:03:007楼 得分:0
没有反应一般是在你的ajax回调事件里把status不等于200的情况给隐藏掉了,试着把status   alert出来看看是不是500,然后显式地去调用一下这个请求看看是什么错误.这样比较好调试
发表于:2007-07-10 18:05:518楼 得分:5
就看到你上传文件的代码,没看到ajax,不知道你怎么处理的,是不是用了微软的ajax框架?
发表于:2007-07-10 18:07:239楼 得分:0
如果是的话更有可能的是路径问题,因为如果你的服务器上装了微软的ajax(你别告诉我你忘记装了),一般来说不会有问题,而在本地正常服务器出问题更可能是路径问题
发表于:2007-07-10 18:37:4210楼 得分:0
我用的是ajax.dll
前台是
function   upload()
{
//debugger;
var   i=document.getelementbyid( "file1 ").value.lastindexof( "\\ ");
var   filename=document.getelementbyid( "file1 ").value.substring(i+1);
//alert(filename);
var   filepath=document.getelementbyid( "file1 ").value;
addproduct.upload(filepath,upload_callback);
//addproduct.(document.up(getelementbyid( "file1 ").value));
}
function   upload_callback(ret)
{
if(ret.value!= " ")
{
document.getelementbyid( "img ").src=ret.value;
}
}


> <input   type= "button "   value= "上传 "   onclick= "upload(); ">
发表于:2007-07-10 18:41:4211楼 得分:0
看看是不是这个原因

http://community.csdn.net/expert/topic/5647/5647156.xml?temp=.7227442
发表于:2007-07-10 19:42:4212楼 得分:0
不行啊,上传到服务器后,如果在服务器本机上运行也没有什么问题
发表于:2007-07-11 09:18:3113楼 得分:0
原因找到了,路径不对,
是不是[ajax.ajaxmethod(ajax.httpsessionstaterequirement.read)]声明了以后server.mappath就取不到路径了
发表于:2007-07-22 10:48:5214楼 得分:15
你的ajax.dll上传文件可以用了吗?我试你的方法,也只能本地测试成功.我想是因为你system.drawing.image   img   =   system.drawing.image.fromfile(fileinfo);的原因吧fileinfo是服务器上的图片路径才对.而本地测试时,fileinfo又是本地又是服务器上,所以能成功.而远程测试的话,服务器根本不存在fileinfo,所以就不行了


快速检索

最新资讯
热门点击