我初学,我以ASP的思路来做的,也不知道.net是什么思路,目地就是要实现如果数据表如果有当前正要上传的的内容时就自动命别名并添加到数据表中,如有很简单的代码请贴上源代码我参考,最好不要太复杂,我看不懂。谢谢! string name = FileUpload1.FileName;
string size = FileUpload1.PostedFile.ContentLength.ToString();
string type1 = FileUpload1.PostedFile.ContentType;
string ipath = Server.MapPath("img\\") + name;
string wpath = "img\\" + name;
if (type1 != "jpg" || type1 != "gif" || type1 != "bmp" || type1 != "png")
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["conn"]);
conn.Open();
SqlCommand cmd1 = new SqlCommand("select * from img where imgpath='"+wpath+"'",conn);
SqlDataReader ds = cmd1.ExecuteReader();
if(ds.Read())
{
ds.Close();
Image1.Visible = true; 自动命名并且上传到img的文件夹下、把路径添加到img表中(表中3个字段,imgid,imgpath,updatetime。 id和updatetime内容是自动获取的,只管添加imgpath就可以了) Label2.Text = "文件上传成功!";
conn.Close();
}
else
{
Image1.Visible = true;
FileUpload1.SaveAs(ipath);
Image1.ImageUrl = wpath;
Label1.Text = "<br>文件名是:" + name + "<br>文件大小是:" + size + "<br>文件类型是:" + type1 + "<br>本地文件路径是:" + ipath + "<br>相当路径是:" + wpath;
SqlCommand cmd = new SqlCommand("insert into img(imgpath) values('" + wpath + "')", conn);
cmd.ExecuteNonQuery();
Label2.Text = "文件上传成功!";
conn.Close();
string size = FileUpload1.PostedFile.ContentLength.ToString();
string type1 = FileUpload1.PostedFile.ContentType;
string ipath = Server.MapPath("img\\") + name;
string wpath = "img\\" + name;
if (type1 != "jpg" || type1 != "gif" || type1 != "bmp" || type1 != "png")
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["conn"]);
conn.Open();
SqlCommand cmd1 = new SqlCommand("select * from img where imgpath='"+wpath+"'",conn);
SqlDataReader ds = cmd1.ExecuteReader();
if(ds.Read())
{
ds.Close();
Image1.Visible = true; 自动命名并且上传到img的文件夹下、把路径添加到img表中(表中3个字段,imgid,imgpath,updatetime。 id和updatetime内容是自动获取的,只管添加imgpath就可以了) Label2.Text = "文件上传成功!";
conn.Close();
}
else
{
Image1.Visible = true;
FileUpload1.SaveAs(ipath);
Image1.ImageUrl = wpath;
Label1.Text = "<br>文件名是:" + name + "<br>文件大小是:" + size + "<br>文件类型是:" + type1 + "<br>本地文件路径是:" + ipath + "<br>相当路径是:" + wpath;
SqlCommand cmd = new SqlCommand("insert into img(imgpath) values('" + wpath + "')", conn);
cmd.ExecuteNonQuery();
Label2.Text = "文件上传成功!";
conn.Close();
string folder = System.Configuration.ConfigurationSettings.AppSettings["PRJDATAFileUpdatePath"];
string path = System.Configuration.ConfigurationSettings.AppSettings["PRJPath"];
if (path == "" || path == null)
{
path = Server.MapPath("");
path = path.Substring(0, path.LastIndexOf(@"\"));
}
path = path + @"\" + folder; if (btn.Text == "重新上傳")
{
LATTACH_FILE1.Visible = false;
FileUpload1.Visible = true;
btn.Text = "上傳";
lbfuploadstatus1.Visible = false;
}
else if (btn.Text == "上傳")
{
if (FileUpload1.HasFile)
{
#region --文件上傳--
try
{
string uploadfilename = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\") + 1,
FileUpload1.PostedFile.FileName.Length - FileUpload1.PostedFile.FileName.LastIndexOf("\\") - 1); string name = PRJ_NOTextBox.Text + "_" + SEQ.Text + "_" + uploadfilename; //==============================Get Pathdir=========================================
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
} //==========================刪除原上傳文件=========================================
if (System.IO.File.Exists(path + @"\" + LATTACH_FILE1.Text))
{
System.IO.File.Delete(path + @"\" + LATTACH_FILE1.Text);
} //=========================刪除與上傳文件同名的文件================================
if (System.IO.File.Exists(path + @"\" + name))
{
System.IO.File.Delete(path + @"\" + name);
} string Savepath = path + @"\" + name;
FileUpload1.SaveAs(Savepath);
LATTACH_FILE1.Text = name;
LATTACH_FILE1.Visible = true;
lbfuploadstatus1.Text = "上傳成功!";
lbfuploadstatus1.Visible = true;
btn.Text = "重新上傳";
FileUpload1.Visible = false;
}
catch
{
lbfuploadstatus1.Text = "上傳失敗!";
lbfuploadstatus1.Visible = true;
}
#endregion
}
else
{
lbfuploadstatus1.Text = "請選擇上傳文件!";
lbfuploadstatus1.Visible = true;
}
}