主题:[讨论]使用upload控件怎么使上传文件重新命名?
使用upload控件怎么使上传文件重新命名?
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string filetype = FileUpload1.PostedFile.ContentType;
if (filetype == "text/plain")
{
try
{
string name = FileUpload1.PostedFile.FileName; // 客户端文件路径
FileInfo file = new FileInfo(name);
string filename = file.Name;
string rndday = System.DateTime.Today.Year.ToString() + System.DateTime.Today.Month.ToString()+System.DateTime.Today.Day.ToString();
string rndnow = System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
string webpath = Server.MapPath("Uploads/")+rndday+rndnow+filename;
FileUpload1.SaveAs(webpath);
Label1.Text = "File name: " +
FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + " kb<br>" + "Content type: " +
FileUpload1.PostedFile.InputStream.Length.ToString();
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
}
else
{
Response.Write("文件类型非法!");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string filetype = FileUpload1.PostedFile.ContentType;
if (filetype == "text/plain")
{
try
{
string name = FileUpload1.PostedFile.FileName; // 客户端文件路径
FileInfo file = new FileInfo(name);
string filename = file.Name;
string rndday = System.DateTime.Today.Year.ToString() + System.DateTime.Today.Month.ToString()+System.DateTime.Today.Day.ToString();
string rndnow = System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
string webpath = Server.MapPath("Uploads/")+rndday+rndnow+filename;
FileUpload1.SaveAs(webpath);
Label1.Text = "File name: " +
FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + " kb<br>" + "Content type: " +
FileUpload1.PostedFile.InputStream.Length.ToString();
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
}
else
{
Response.Write("文件类型非法!");
}
}