服务器中上传图片的奇怪问题

charlie1190 2010-11-22 04:58:28
一个上传图片的页面upload.aspx 在本机IIS调试成功。但是把源码文件上传到购买的服务器上,却出现了上传失败,请重新上传字样。特别奇怪的是这个文件在出现上传失败的提示后,在服务器指定文件夹中去看,其实是已经上传到服务器中了。下面给出代码,请高手们帮忙分析下。

upload.aspx文件


<script type="text/javascript">
var uploadSelect = function(el)
{
var fileSrc = document.getElementById("file1").value;
fileSrc = fileSrc.split('.');
fileSrc = fileSrc[fileSrc.length - 1].toLowerCase();
fileInfo = '只能上传 jpg,gif,bmp,png 格式的图片.';
if(fileSrc == "gif" || fileSrc == "png" || fileSrc == "jpge" || fileSrc == "jpg" || fileSrc == "bmp"){}else{alert(fileInfo);return};
el.fadeOut("show");
parent.uploading(document.getElementById("file1").value);
//shop_admin_upload.CheckImg(obj.value,get_szsq);
$("#frmUpload").submit();
};


</script>
</head>
<body>
<form runat="server" id="frmUpload" method="post" enctype="multipart/form-data">
<input style="width:230px;height:20px;border:1px solid #5F5E5E;" type="file" runat="server" id="file1" size="40" onchange="uploadSelect($(this));" />
</form>



upload.aspx.cs文件

public partial class shop_admin_upload : System.Web.UI.Page
{
pub mypub = new pub();
string picPath = "";
string picServer = "/uploadpics";
water mywater = new water();
protected string itemID = "";
protected void Page_Load(object sender, EventArgs e)
{
pub.CheckLogin(mypub.GetSubCookie("user", "hyloginstate"));
if (Request.QueryString["id"] != null)
{
itemID = Request.QueryString["id"];
}
if (IsPostBack)
{
picPath = Server.MapPath("\\uploadpics");
doUpload();
}
}

protected void doUpload()
{
try
{
HttpPostedFile file = file1.PostedFile;
string ext = GetExtension(file.FileName); //取得扩展名
//ext = ext.ToLower();
int filesize = file.ContentLength;
if ((ext != ".gif") && (ext != ".jpg") && (ext != ".jpeg") && (ext != ".JPG") && (ext != ".JPEG") && (ext != ".BMP") && (ext != ".bmp") && (ext != ".png") && (ext != ".PNG"))
{
WriteJs("parent.uploaderror_extention();");
}
//判断文件大小是否超过500KB
else if (filesize > 500*1024)
{
WriteJs("parent.uploaderror_size();");
}
else
{
string strNewPath = GetSaveFilePath() + ext;
file.SaveAs(picPath + strNewPath);
string urlPath = picServer + strNewPath;
urlPath = urlPath.Replace("\\", "/");
string urlPathNew = urlPath.Replace(".", "h.");

string webFileYuantu = Server.MapPath(urlPath);
string webFilePath_small = Server.MapPath(pub.GetSmallTp(urlPathNew));  // 服务器端缩略图路径
string webFilePath_water = webFileYuantu.Replace(".", "h."); ; // 服务器端带水印图路径(图片) 作为最终图片
string webFilePath_font = webFileYuantu.Replace(".", "h.");
string webFilePath_shuiyin = Server.MapPath(water.strshuiyinpic); // 服务器端水印图路径(图片)
water.MakeSmallPic(webFileYuantu, webFilePath_small, 130, 110, "Cute");
water.MakeWaterPic(webFileYuantu, webFilePath_water, webFilePath_shuiyin);
//string font_name = "李亚";
//string font_tel = "13916886188";
//water.MakeWaterFont(webFileYuantu, webFilePath_font, "上传:"+font_name+"Tel:"+font_tel);
water.DeleteMyFile(webFileYuantu);
WriteJs("parent.uploadsuccess('" + urlPathNew + "','" + itemID + "'); ");
}

}
catch (Exception ex)
{
WriteJs("parent.uploaderror();");
}
}

private string GetExtension(string fileName)
{
try
{
int startPos = fileName.LastIndexOf(".");
string ext = fileName.Substring(startPos, fileName.Length - startPos);
return ext;
}
catch (Exception ex)
{
WriteJs("parent.uploaderror('" + itemID + "');");
return string.Empty;
}
}

private string GetSaveFilePath()
{
try
{
DateTime dateTime = DateTime.Now;
string yearStr = dateTime.Year.ToString(); ;
string monthStr = dateTime.Month.ToString();
string dayStr = dateTime.Day.ToString();
string hourStr = dateTime.Hour.ToString();
string minuteStr = dateTime.Minute.ToString();
string straa = dateTime.ToString(@"\\yyyyMM");
string dir = dateTime.ToString(@"\\yyyyMMdd");
if (!Directory.Exists(picPath + straa + dir))
{
Directory.CreateDirectory(picPath +straa+ dir);
}
return straa + dir + dateTime.ToString("\\\\yyyyMMddhhmmssffff");
}
catch (Exception ex)
{
WriteJs("parent.uploaderror();");
return string.Empty;
}
}

protected void WriteJs(string jsContent)
{
this.Page.RegisterStartupScript("writejs","<script type='text/javascript'>"+ jsContent+"</script>");
}

}


上传调用文件

<script type="text/javascript">
var jQuery=$;
var photocount=0;
jQuery(document).ready(function(){
uploadcreate(jQuery("#uploadbox"));
});

var hideDiv = function(idName)
{
jQuery("#"+idName).fadeOut("fast");
};

//载入中的GIF动画
var loadingUrl = "/images/ajax-loader.gif";

//选择文件后的事件,iframe里面调用的
var uploading = function(imgsrc){
var el = jQuery("#uploading");
jQuery("#ifUpload").fadeOut("fast");
el.fadeIn("fast");
el.html("<img src='"+loadingUrl+"' align='absmiddle' /> 上传中...");
return el;
};

//重新上传方法
var uploadinit = function(){
jQuery("#uploading").fadeOut("fast",function(){
jQuery("#ifUpload").fadeIn("fast");
});
};

//上传时程序发生错误时,给提示,并返回上传状态
var uploaderror = function(){
alert("上传失败,请您重新上传!");
uploadinit();
};
//文件太大
var uploaderror_size = function()
{
alert("文件大小不能超过500KB!");
uploadinit();
};

//文件后缀
var uploaderror_extention = function()
{
alert("图片文件必须为jpg,gif,bmp,png格式!");
uploadinit();
};
//上传成功后的处理
var uploadsuccess = function(newpath)
{
//$("#MyText").append(""+newpath+"");
var str = '<span style="width:100px;display:block;float:left;margin-right:4px;" class=\"newpic\" ><input type="hidden" value="'+newpath+'" name="MyPic'+photocount+'" id="Hidden'+photocount+'" > <a href="'+newpath+'" target=_blank><img class="photo" width="100" height="80" src="'+newpath+'" name="File'+photocount+'" /></a><br/><textarea type="text" class="txtarea" rows="2" name="MyText'+photocount+'" value=\"请在这边填写描述\" style=\"width:100px;background:url(/images/tpms_bg.gif) no-repeat;overflow:hidden;border:1px solid #808080; margin-left:0;\"></textarea><br /><span style="display:none" id="FangxingSpan'+photocount+'"><input onkeyup="chkfloat(this);" style="width:28px;border:1px solid #808080; " id="MyShi'+photocount+'" name="MyShi'+photocount+'">室 <input onkeyup="chkfloat(this);" style="width:28px;border:1px solid #808080; " id="MyTing'+photocount+'" name="MyTing'+photocount+'">厅<br></span> <select onchange="javascript:changeMySelect(this,'+photocount+');" id="MySelect'+photocount+'" name="MySelect'+photocount+'"><option value="shineitu">室内图</option><option value="xiaoqutu">小区图</option><option value="huxingtu">户型图</option><option value="zhanshitu">展示图</option></select> <a href="javascript:;" onclick="javascript:delSpan(jQuery(this))">删除</a></span>'
document.getElementById('Div1').insertAdjacentHTML("beforeEnd",str);
var el = jQuery("#uploading");
photocount++;
if(photocount<=7)
{
//el.html("上传成功,<a href=\"javascript:void(0);\" onclick=\"uploadinit();\">[继续上传]</a>.");
uploadinit();
jQuery("#NoPhoto").remove();
}
else
{
el.html("上传成功,<a href=\"javascript:void(0);\" onclick=\"uploadinit();\"></a>最多只能上传8张.");
jQuery("#NoPhoto").remove();
}
};
//创建一个上传控件
var uploadcreate = function(el){
var strContent ="";
var strContent = "<div class='uploadcontrol'><iframe src=\"upload.aspx\" id=\"ifUpload\" frameborder=\"no\" scrolling=\"no\" style=\"width:260px; height:28px;\"></iframe>";
strContent += "<div class=\"uploading\" id=\"uploading\" style=\"display:none;\" ></div>";
strContent += "<div class=\"image\" id=\"panelViewPic\" style=\"display:none;\"></div></div>";
el.append(strContent);
...全文
179 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
fendouaj 2010-11-23
  • 打赏
  • 举报
回复
你贴出来的代码,有的方法都没有,根本不好找出错在哪里啊!比如:uploadinit(),DeleteMyFile()这些方法都没有啊!
chen_ya_ping 2010-11-23
  • 打赏
  • 举报
回复
你的catch部分,是扑捉到了错误,但是我想,你还是把你的错误抛出吧。要不然就不知道真正的错误,就算不抛出,也能够让人知道错误。知道了异常,就好办了。
truecoffeefox 2010-11-23
  • 打赏
  • 举报
回复
咋没懂我意思捏
catch (Exception ex)
{
WriteJs("parent.uploaderror();");
}
}
你改下uploaderror函数将ex的message弹出来不就可以知道错在哪了
charlie1190 2010-11-22
  • 打赏
  • 举报
回复
不应该是目录权限问题,因为图片是能够上传到服务器UPLOADFILE文件夹的,只是程序会报错,而且报错引用的是uploaderror = function() 非大小和文件类型的错误,不知道是什么原因。ALERT跟踪也显示不出来。
flyerwing 2010-11-22
  • 打赏
  • 举报
回复
有可能是上传目录权限问题吧.
不知道是不是了.菜鸟瞎说的
charlie1190 2010-11-22
  • 打赏
  • 举报
回复
有高手可以为我解答吗?
charlie1190 2010-11-22
  • 打赏
  • 举报
回复
WriteJs("parent.uploaderror('" + itemID + "');");

去掉了参数问题依然存在耶
charlie1190 2010-11-22
  • 打赏
  • 举报
回复
这个问题哦 是这个样子的, 上传的水印我是用图片的形式加上去的,因此不会有字体的问题。其次这个上传完后,服务器的文件夹中的图片就是打好水印了的。因此说明水印功能是已经正常使用了的。
WriteJs("parent.uploaderror('" + itemID + "');");
这个东西加了参数应该也不会有问题的吧。
一克代码 2010-11-22
  • 打赏
  • 举报
回复
WriteJs("parent.uploaderror('" + itemID + "');");


这为什么要加参数?
边城的刀声 2010-11-22
  • 打赏
  • 举报
回复
上传没有问题,是你的水印那部分代码出问题了,服务器上没有你水印字用的字体,换个字体试试。
truecoffeefox 2010-11-22
  • 打赏
  • 举报
回复
可能加水印过程出错吧,调试的时候alert异常的信息看
charlie1190 2010-11-22
  • 打赏
  • 举报
回复
但是这个程序在本机2000以及2003中调试通过,上传到购买的服务器空间中,就出现了这个问题。
一克代码 2010-11-22
  • 打赏
  • 举报
回复
这给空间没有关系吧!
BBSMAX 5.0.1 是一款基于ASP.NET技术的开源论坛程序。BBSMAX 5.0.1 内建支持博客, 相册,微博等常见SNS功能,为在Windows服务器上架设社区网站提供完美的解决方案。BBSMAX 5.0.1 拥有世界一流的用户体验,最先进的论坛管理方式,卓越的访问速度和负载能力... BBSMAX 5.0.1 源码包 更新日志:2010-10-08 1、修正了升级向导可能出错的问题。 2、修正了JS远程调用无效的问题。 3、修正了附件大图片无法全显示的BUG。 4、修正了验证码点击刷新出错。 5、修正了移动板块主题分类的顺序后照成分类下的主题不显示的BUG。 6、修正了其他已知的BUG。 BBSMAX是国内发展最早的基于.net技术构建的bbs,在leobbs独步天下、微软.net刚刚诞生的时候,BBSMAX就开始发展了(当时名为 nowboard),历经多年发展,从个人开发到团队开发,从业余开发到全职开发,BBSMAX经历了质的蜕变。现在BBSMAX的开发团队人员齐整、分工明确,从项目策划、到程序开发、到客户端开发、到设计、到技术支持一应俱全,我们就是要高起点、做好一件事。 当前国内的BBS应用依然以php为主导,在linux/unix下php确实是个不错的脚本语言。但是有个非常奇怪的现象:90%的用户都在windows服务器上运行php mysql开发的bbs。有一个很重要的原因,就是其它语言迄今为止还没有出现一个强有力的程序,让用户相信其他语言 可以做的更好。我们希望,BBSMAX的出现能对.net程序在广大站长群体的进一步普及贡献绵薄之力,我们更希望,让所有windows服务器平台的用户有更好的选择!

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧