问题一、直接运行值能上传上去一张图片、
问题二、单步调试运行 可以上传三张图片、但是保存下来的图片全是第一张(单步调试每一步都正确、不存在文件名重复)有解决方案后台代码、前台就不用发了吧
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
this.shangChuan1("image1", this.FileUpload1);
this.shangChuan1("image1", this.FileUpload2);
this.shangChuan1("image1", this.FileUpload3);
} //图片上传1
public string shangChuan1(string path,FileUpload fu)
{
string filename = "";
try
{
string filepath = fu.PostedFile.FileName; //得到的是文件的完整路径,包括文件名,如:C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg
string houzuiming = filepath.Substring(filepath.LastIndexOf("."));
if (houzuiming == ".BMP" || houzuiming == ".JPG" || houzuiming == ".bmp" || houzuiming == ".jpg" || houzuiming == ".gif" || houzuiming == ".GIF" || houzuiming == ".png" || houzuiming == ".PNG")
{
filename = DateTime.UtcNow.ToString("yyyy" + "MM" + "dd" + "HH" + "mm" + "ss" + "fff") + houzuiming;//20022775_m.jpg
string serverpath = Server.MapPath("~/" + path + "/") + filename;//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg
this.FileUpload1.PostedFile.SaveAs(serverpath);//将上传的文件另存为
}
else
{
//;
}
}
catch (Exception ex)
{ throw ex;
}
return filename;
}
问题二、单步调试运行 可以上传三张图片、但是保存下来的图片全是第一张(单步调试每一步都正确、不存在文件名重复)有解决方案后台代码、前台就不用发了吧
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
this.shangChuan1("image1", this.FileUpload1);
this.shangChuan1("image1", this.FileUpload2);
this.shangChuan1("image1", this.FileUpload3);
} //图片上传1
public string shangChuan1(string path,FileUpload fu)
{
string filename = "";
try
{
string filepath = fu.PostedFile.FileName; //得到的是文件的完整路径,包括文件名,如:C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg
string houzuiming = filepath.Substring(filepath.LastIndexOf("."));
if (houzuiming == ".BMP" || houzuiming == ".JPG" || houzuiming == ".bmp" || houzuiming == ".jpg" || houzuiming == ".gif" || houzuiming == ".GIF" || houzuiming == ".png" || houzuiming == ".PNG")
{
filename = DateTime.UtcNow.ToString("yyyy" + "MM" + "dd" + "HH" + "mm" + "ss" + "fff") + houzuiming;//20022775_m.jpg
string serverpath = Server.MapPath("~/" + path + "/") + filename;//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg
this.FileUpload1.PostedFile.SaveAs(serverpath);//将上传的文件另存为
}
else
{
//;
}
}
catch (Exception ex)
{ throw ex;
}
return filename;
}
protected void Button1_Click(object sender, EventArgs e)
{
this.shangChuan1("image1", this.FileUpload1);
this.shangChuan1("image1", this.FileUpload2);
this.shangChuan1("image1", this.FileUpload3);
}
image1呢 具体是什么呢
不对, 我发现了。 LZ 这里
this.FileUpload1.PostedFile.SaveAs(serverpath);//将上传的文件另存为
你应该把 this.FileUpload1 换为 参数 fu
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string name = FileUpload1.FileName;//上传文件名字
string size = FileUpload1.PostedFile.ContentLength.ToString();
string type = FileUpload1.PostedFile.ContentType;
string type2 = name.Substring(name.LastIndexOf(".") + 1);
string ipath = Server.MapPath("upimg") + "\\" + name;
string fpath = Server.MapPath("upfile") + "\\" + name;
string path="F:\\aaa\\"+FileUpload1.FileName;
string wpath = "upimg\\" + name;
if (type2 == "jpg" || type2 == "gif" || type2 == "bmp" || type2 == "png")
{
FileUpload1.SaveAs("F:\\aaa\\"+FileUpload1.FileName);
// Image1.ImageUrl="F:\\aaa\\"+FileUpload1.FileName;
Label1.Text = "你传图片的名字是" + name + "<br>文件大小为" + size + "<br>文件类型为" + type2 + "<br>文件路径为" + ipath;
}
SqlConnection cn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");
SqlCommand cmd = new SqlCommand("insert into Image(imageName,imagepath) values('" + name + "','" + path + "')", cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{ SqlConnection cn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");
SqlCommand cmd = new SqlCommand("select imageName from Image where imageID='" + Convert.ToInt32(TextBox1.Text) + "'", cn);
cn.Open();
string a = cmd.ExecuteScalar().ToString();
cn.Close();
Image1.ImageUrl = "F:\\aaa\\" + a;
}
}
当然只保存第一张了。
应该为:
this.[color=#FF0000]fu.PostedFile.SaveAs(serverpath);[/color]
这是啥啊,你应该用fu吧这里改了如果不稳定那可能是文件名生成一个了,换guid再试下
文件名没问题、断点一步一步调试完全都正常的、 guid是神马。