asp.net怎样获取HTML表单File中的路径
2013-08-19 11:49
267 查看
#region 上传文件到数据库和服务器
public void FN_UpFiles()
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName = "";//定义文件名
//string fileExtension = "";
fileName = Path.GetFileName(postedFile.FileName);//得到上传文件的完整名称 即文件名+后缀名
int index = fileName.IndexOf(".");
string FileType = fileName.Substring(index).ToLower();//截取文件后缀名
//FileTypeImg = "../FileTypeimg/" + hz + ".gif";
Guid fileGuid = Guid.NewGuid();//生成新的文件名称 以GUID命名防止文件名相同
string NewFileName = fileGuid.ToString();//新的文件名
NewFileName = NewFileName + FileType;//新的文件名+后缀名
if (postedFile.ContentLength > 2097151 * 1024)//判断是否大于配置文件中的上传文件大小
{
Page.RegisterStartupScript("提示", "<script language='javascript'>alert('对不起您的上传资源过大!');return;</script>");
return;
}
else
{
if (fileName != "")//如果文件名不为空
{
try
{
//文件虚拟路径
string strpath = System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName;
try
{
NRModel.File model = new NRModel.File();
NRBLL.File bf = new NRBLL.File();
Guid guid1 = Guid.NewGuid();
Guid guid2 = new Guid(FolderId);
Guid guid3 = Guid.NewGuid();
Guid guid4 = Guid.NewGuid();
model.Fileid = guid1;
model.Folderid = guid2;
model.Filepath = strpath;
model.FileNam = fileName;
model.FileSize = postedFile.ContentLength;
model.Decription = TextArea1.Value.ToString();
model.CreateOn = DateTime.Now;
model.CreateBy = guid3;
model.ModefyBy = guid4;
if (bf.FN_AddNewRes(model) > 0)
{
NR.Error.Log.LogType("上传资源" + fileName + "成功!" + "服务器路径:" + strpath);
//保存文件到指定目录(虚拟目录)
postedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName);
//Page.RegisterStartupScript("提示", "<script language='javascript'>alert('上传成功!');self.opener.location.reload();window.close();</script>");
AlertMsg("上传成功!");
}
}
catch (Exception ex)
{
NR.Error.Log.LogType(ex.ToString());
}
}
catch (Exception ex)
{
NR.Error.Log.LogType(ex.ToString());
}
}
else
{
Response.Write("上传文件不能为空!");
NR.Error.Log.LogType("文件不能为空!");
}
}
}
}
catch (System.Exception ex)
{
NR.Error.Log.LogType(ex.ToString());
}
}
#endregion
public void FN_UpFiles()
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName = "";//定义文件名
//string fileExtension = "";
fileName = Path.GetFileName(postedFile.FileName);//得到上传文件的完整名称 即文件名+后缀名
int index = fileName.IndexOf(".");
string FileType = fileName.Substring(index).ToLower();//截取文件后缀名
//FileTypeImg = "../FileTypeimg/" + hz + ".gif";
Guid fileGuid = Guid.NewGuid();//生成新的文件名称 以GUID命名防止文件名相同
string NewFileName = fileGuid.ToString();//新的文件名
NewFileName = NewFileName + FileType;//新的文件名+后缀名
if (postedFile.ContentLength > 2097151 * 1024)//判断是否大于配置文件中的上传文件大小
{
Page.RegisterStartupScript("提示", "<script language='javascript'>alert('对不起您的上传资源过大!');return;</script>");
return;
}
else
{
if (fileName != "")//如果文件名不为空
{
try
{
//文件虚拟路径
string strpath = System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName;
try
{
NRModel.File model = new NRModel.File();
NRBLL.File bf = new NRBLL.File();
Guid guid1 = Guid.NewGuid();
Guid guid2 = new Guid(FolderId);
Guid guid3 = Guid.NewGuid();
Guid guid4 = Guid.NewGuid();
model.Fileid = guid1;
model.Folderid = guid2;
model.Filepath = strpath;
model.FileNam = fileName;
model.FileSize = postedFile.ContentLength;
model.Decription = TextArea1.Value.ToString();
model.CreateOn = DateTime.Now;
model.CreateBy = guid3;
model.ModefyBy = guid4;
if (bf.FN_AddNewRes(model) > 0)
{
NR.Error.Log.LogType("上传资源" + fileName + "成功!" + "服务器路径:" + strpath);
//保存文件到指定目录(虚拟目录)
postedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName);
//Page.RegisterStartupScript("提示", "<script language='javascript'>alert('上传成功!');self.opener.location.reload();window.close();</script>");
AlertMsg("上传成功!");
}
}
catch (Exception ex)
{
NR.Error.Log.LogType(ex.ToString());
}
}
catch (Exception ex)
{
NR.Error.Log.LogType(ex.ToString());
}
}
else
{
Response.Write("上传文件不能为空!");
NR.Error.Log.LogType("文件不能为空!");
}
}
}
}
catch (System.Exception ex)
{
NR.Error.Log.LogType(ex.ToString());
}
}
#endregion
相关文章推荐
- asp.net获取HTML表单File中的路径的方法
- IE7,IE8,上传文件控件input file和asp.net FileUpload控件无法获取完整路径的解决办法
- asp.net 中出现<input type="file"/> 控件中获取不到全路径
- ASP.Net 类(CS)文件怎样获取Web应用程序的路径
- 怎样在ASP.Net 的.aspx.cs页面中获取html的标签
- Asp.Net - 获取FileUpload控件的文件路径、文件名、扩展名
- asp.net后台获取路径的各种方法归纳
- Asp.net中获取应用程序完整Url路径的方法
- .Net技巧:ASP.NET获取网站路径
- ASP.NET获取服务器文件的物理路径
- HttpContext是干什么的; asp.net 获取网站路径;DataSet读取xml文件;updatepanel 控件的局部刷新 always和condition
- php HTML file 获取绝对路径
- C#(ASP.Net)获取当前路径的方法集合
- Asp.NET获取文件及其路径
- ASP.NET Web Pages - HTML 表单
- ASP.NET常用路径获取方法与格式对照表
- asp.net后台获取前台html控件的值的方法
- Asp.net中从后台中如何获取html控件的值?
- Asp.Net中CS中获取物理路径