获取FileUpload中的上传文件路径
2012-02-01 22:10
225 查看
//单击事件,完成上传文件到服务器
private void btnUpload_Click(object sender, System.EventArgs e)
{
if(fileUpload1.PostedFile!=null && fileUpload1.PostedFile.FileName!="" && fileUpload1.PostedFile.ContentLength!=0)
{
if(fileUpload1.PostedFile.ContentLength > 5 * 1024 )
{
lblError.Text = "上传文件过大";
lblError.Visible = true;
return;
}
string sFileName = Path.GetExtension(fileUpload1.PostedFile.FileName).ToUpper();
if(! (strFileName == ".BMP" || strFileName == ".GIF" || strFileName == ".JPG") )
{
lblError.Text = "文件格式不正确";
lblError.Visible = true;
return;
}
Random ran = new Random();
string sNewImg = DateTime.Now.ToString(@"yyyyMMddHHmmss") + ran.Next(100,999) + Path.GetExtension(fileUpload1.PostedFile.FileName) ;
string sPath = Server.MapPath( "~/Picture/" + sNewImg);
if( !Directory.Exists(Path.GetDirectoryName(sPath) ) )
{
Directory.CreateDirectory(Path.GetDirectoryName(strPath));
}
fileUpload1.PostedFile.SaveAs(sPath);
}
}
其中,"fileUpload1.PostedFile.FileName"便是文本框中的路径!
private void btnUpload_Click(object sender, System.EventArgs e)
{
if(fileUpload1.PostedFile!=null && fileUpload1.PostedFile.FileName!="" && fileUpload1.PostedFile.ContentLength!=0)
{
if(fileUpload1.PostedFile.ContentLength > 5 * 1024 )
{
lblError.Text = "上传文件过大";
lblError.Visible = true;
return;
}
string sFileName = Path.GetExtension(fileUpload1.PostedFile.FileName).ToUpper();
if(! (strFileName == ".BMP" || strFileName == ".GIF" || strFileName == ".JPG") )
{
lblError.Text = "文件格式不正确";
lblError.Visible = true;
return;
}
Random ran = new Random();
string sNewImg = DateTime.Now.ToString(@"yyyyMMddHHmmss") + ran.Next(100,999) + Path.GetExtension(fileUpload1.PostedFile.FileName) ;
string sPath = Server.MapPath( "~/Picture/" + sNewImg);
if( !Directory.Exists(Path.GetDirectoryName(sPath) ) )
{
Directory.CreateDirectory(Path.GetDirectoryName(strPath));
}
fileUpload1.PostedFile.SaveAs(sPath);
}
}
其中,"fileUpload1.PostedFile.FileName"便是文本框中的路径!
相关文章推荐
- FileUpload上传文件时,为何不能获取到完整路径
- IE7,IE8,上传文件控件input file和asp.net FileUpload控件无法获取完整路径的解决办法
- FileUpload 控件上传文件路径获取不到。
- 20160620001 FileUpload控件获取上传文件的路径
- FileUpload上传文件时,为何不能获取到完整路径
- FileUpload控件获取要上传文件的路径
- js获取上传文件的绝对路径
- js获取上传文件的绝对路径
- 加载上传图片,获取文件名、文件路径并保存到程序的文件夹中
- 上传文件 获取路径
- CSS美化上传按钮并获取上传文件路径
- JS 获取IE上传文件路径(IE7,8)
- CuteEditor文件上传获取具体路径并写入数据库
- asp.net上传文件使用fileupload控件,判断文件类型和大小,取得文件路径时报错【System.IO.FileNotFoundException:未能找到文件】的解决办法
- JS获取上传文件的绝对路径,兼容IE和FF
- 怎么用js来获取 fileupload中的上传文件的文件名。
- 根据FileUpload控件名获取上传文件(大小)类型
- WebUploader获取文件上传成功路径问题
- JS获取IE上传文件路径、后缀、大小、文件名代码
- 使用ServletFileUpload 实现文件上传 和获取文件信息