string fileName1 = this.FileUploadAddrPic.FileName;
FileUploadAddrPic.SaveAs(Server.MapPath("~\\Prod_Images") + "\\" + fileName1);
hotel.HotelAddressImage = "~\\Prod_Images" + "\\" + fileName1;
为什么fileName1一直为空,之前测试通过,现在就是报错。
因为fileName为空,所以下面的SaveAs一直找不到文件!
帮忙,B/S学的少。
解决方案 »
- .net如何设定默认页
- 三层开发中的 调用失效问题
- 网页问题
- SSL问题.大灰狼,孟子,你们快来帮忙啊....我心虚啊.....急死了............快啊.....
- 大清早 一个简单的控件问题 深情的等你
- 试图运行项目是出错:无法在Web服务器上启动调试,你不具备调试此应有程序的权限,此项目的URL位于Internet区域。
- 大家看下,这样的集合怎么做。~~~~~~~~~~~
- request无法读取到传递来的值
- FootRoww.FindControl出来的值为NULL,怎么改代码?
- Server.MapPath("/db/aaa.mdb")这样写有问题???
- 在script中如何定义数据库中的一些值
- 算法求解
file_time = DateTime.Now.Year.ToString + DateTime.Now.Month.ToString + DateTime.Now.Day.ToString + DateTime.Now.Hour.ToString + DateTime.Now.Minute.ToString + DateTime.Now.Second.ToString + DateTime.Now.Millisecond.ToString()
以上只是取得特定的文件名,应该这样子来:
string fileName1 = this.FileUploadAddrPic.FileName.ToString();//取得特定文件名
FileInfo f_into = new FileInfo(fileName1);//取得文件信息
string f_name = f_into.Name.ToString();//取得文件名;
FileUploadAddrPic.SaveAs(Server.MapPath("~\\Prod_Images") + "\\" + f_name);
hotel.HotelAddressImage = "~\\Prod_Images" + "\\" + f_name;
其中fileName1=this.FileUploadAddrPic.FileName.ToString();//取得特定文件名FileInfo
修改为fileName1=this.FileUploadAddrPic.PostedFile.FileName.ToString();//取得特定文件名FileInfo
protected void Button1_Click(object sender, EventArgs e)
{
H_Hotel hotel = new H_Hotel();
hotel.HotelSN = txtHotelSN.Text.ToString().Trim();
hotel.HotelName = txtHotelName.Text.ToString().Trim();
hotel.HotelAddress = txtHotelAddr.Text.ToString().Trim();
hotel.HotelIntroduce = txtHotelIntr.Text.ToString().Trim();
hotel.IsRecommend = Convert.ToInt32(txtRecommend.Text.ToString().Trim());
hotel.AvePrice = txtPrice.Text.ToString().Trim();
hotel.MemberTel = txtMemeberNumber.Text.ToString().Trim(); string fileName1 = this.FileUploadAddrPic.PostedFile .FileName.ToString();
//跟踪这里的fileName1 为空!
FileUploadAddrPic.SaveAs(Server.MapPath("~\\Prod_Images") + "\\" + fileName1);
hotel.HotelAddressImage = "~\\Prod_Images" + "\\" + fileName1; string fileName2 = this.FileUploadHotelPic.FileName.ToString();
FileUploadHotelPic.SaveAs(Server.MapPath ("~\\Prod_Images") + "\\" + fileName2);
hotel.HotelPicture = "~\\Prod_Images" + "\\" + fileName2;
IUDMethed iud = new IUDMethed();
if (type == "1")
{
iud.AddHotel(hotel);
}
else
{
iud.UpdateHotel(hotel);
}
}
可以吗?
你是不是在aspx里使用js处理过了?
string fileName1 = this.FileUploadAddrPic.PostedFile .FileName.ToString();
string fileName1 = this.FileUploadAddrPic.FileName.ToString();
string fileName1 = this.FileUploadAddrPic.FileName;以上三中方式现在都不行,
我很奇怪,为什么之前可以,现在突然就出现这样的问题呢?
<asp:FileUpload ID="FileUploadHotelPic" runat="server" /></td>
多此一举,FileName本身就是String类型了。
一:iis中是否允许你上传你的文件类型;
二:.net默认上传不超过4M的文件。
是因为fileName为空,所以下面的SaveAs一直找不到文件!可能是编译器出错了也说不定,重启一下电脑。呵呵
我丢人了,闪了。
if(!this.FileUploadAddrPic.HasFile){}
2. 取得上传文件名称直接 使用 string fileName1 = this.FileUploadAddrPic.FileName;
就可以
3. 发一个成功使用的图片上传例子你参考一下
//上传营业执照
<asp:FileUpload ID="imgfile" runat="server" />
protected void Button3_Click(object sender, EventArgs e)
{
if (!imgfile.HasFile)//是否上传图片
{
isUploadImg=false;
Response.Write(string.Format(JsHelper.WINALERTSCRIPT, "请上传图片"));
//Response.Write("<script>alert('请上传图片')</script>");
//RegisterStartupScript("", "<script language=\"javascript\">window.alert('请上传图片!')</script>");
}
else
{
//图片格式限制 bmp/jpg/gif
if (this.imgfile.PostedFile.ContentType == "image/bmp" || this.imgfile.PostedFile.ContentType == "image/jpg" || this.imgfile.PostedFile.ContentType == "image/gif" || this.imgfile.PostedFile.ContentType == "image/pjpeg")
{
//图片大小限制小于3MB
if (this.imgfile.PostedFile.ContentLength > 1024 * 3072)
{
Response.Write(string.Format(JsHelper.WINALERTSCRIPT, "上传图片太大,图片大小应小于3MB"));
}
else
{
//Response.Write(string.Format(JsHelper.WINALERTSCRIPT, "文件路径" + this.imgfile.PostedFile.FileName + "图片类型" + this.imgfile.PostedFile.ContentType + "图片大小" + this.imgfile.PostedFile.ContentLength));
string path = Server.MapPath("~/Superdisplay/zhengshu/");
string filePath = path + this.imgfile.FileName; //判断图片是否存在
FileInfo nowfile = new FileInfo(filePath);
if (nowfile.Exists)
{
Response.Write(string.Format(JsHelper.WINALERTSCRIPT, "图片已经存在,请换张图片,或修改该图片名称"));
this.imgShow.Visible = false;
}
else
{
try
{
this.imgfile.SaveAs(path + this.imgfile.FileName);
Response.Write(string.Format(JsHelper.WINALERTSCRIPT, "上传营业执照图片成功"));
ViewState["sellCard"] = "~/Superdisplay/zhengshu/" + imgfile.FileName;
this.imgShow.Visible = true;
this.imgShow.ImageUrl = "~/Superdisplay/zhengshu/" + imgfile.FileName;
}
catch (Exception ex)
{
Response.Write(string.Format(JsHelper.WINALERTSCRIPT, "图片上传失败,请重新上传"));
} }
}
}
else
{
Response.Write(string.Format(JsHelper.WINALERTSCRIPT, "图片格式只能为bmp或jpg或gif"));
}
}