服务器中上传图片的奇怪问题
一个上传图片的页面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);