DataGrid中的HtmlInputFile上传文件
2009-09-16 09:43
309 查看
HTML控件加上runat="server"属性后就可以在后台控制,这样可以大大简化前台js代码的编写。
如题下面是我写的DataGrid中的HtmlInputFile上传文件的例子
(1)首先是提交按钮的代码
//实例化前台 imagesFile
HtmlInputFile fileUpLoadImages = (HtmlInputFile)gvr.Cells[0].FindControl("imagesFile");
string strimageUrl="";
//上传附件
if(fileUpLoadImages.Value!="")
{
bool blPicture=true;
//判断文件大小如果大小为0,则提示上传失败
if(fileUpLoadImages.PostedFile.ContentLength<1)
{
Response.Write("<script>window.alert('文件大小为零,上传失败!')</script>");
}
else
{
//加时间戳
string strTempName=DateTime.Now.Ticks.ToString();
HttpPostedFile upFile=fileUpLoadImages.PostedFile;
int FileLength=upFile.ContentLength; //文件大小
string strFilePath=upFile.FileName; //文件全名称
int intExtNamePos=strFilePath.LastIndexOf(".");
int intPosition=strFilePath.LastIndexOf('//');
//删除函数删除.前文件名
string AttachFileName=strFilePath.Remove(0,intPosition+1); //文件名
string AttachExtName=strFilePath.Remove(0,intExtNamePos+1); //扩展名
string UploadFileDestination; //上传路径
UploadFileDestination =“你要保存的路径”
try
{
//判断是否有该目录
if(!Directory.Exists(UploadFileDestination))
{
//如果没有就建立一个目录
Directory.CreateDirectory(UploadFileDestination);
}
//保存图片
upFile.SaveAs(UploadFileDestination+strTempName+"."+AttachExtName);
}
catch
{
Response.Write("<script>window.alert('上传附件失败!')</script>");
blPicture=false;
}
}
}
}
(2) 前台的HtmlInputFile控件一定要加上runat="server" 这样后台才能获取 。
如题下面是我写的DataGrid中的HtmlInputFile上传文件的例子
(1)首先是提交按钮的代码
//实例化前台 imagesFile
HtmlInputFile fileUpLoadImages = (HtmlInputFile)gvr.Cells[0].FindControl("imagesFile");
string strimageUrl="";
//上传附件
if(fileUpLoadImages.Value!="")
{
bool blPicture=true;
//判断文件大小如果大小为0,则提示上传失败
if(fileUpLoadImages.PostedFile.ContentLength<1)
{
Response.Write("<script>window.alert('文件大小为零,上传失败!')</script>");
}
else
{
//加时间戳
string strTempName=DateTime.Now.Ticks.ToString();
HttpPostedFile upFile=fileUpLoadImages.PostedFile;
int FileLength=upFile.ContentLength; //文件大小
string strFilePath=upFile.FileName; //文件全名称
int intExtNamePos=strFilePath.LastIndexOf(".");
int intPosition=strFilePath.LastIndexOf('//');
//删除函数删除.前文件名
string AttachFileName=strFilePath.Remove(0,intPosition+1); //文件名
string AttachExtName=strFilePath.Remove(0,intExtNamePos+1); //扩展名
string UploadFileDestination; //上传路径
UploadFileDestination =“你要保存的路径”
try
{
//判断是否有该目录
if(!Directory.Exists(UploadFileDestination))
{
//如果没有就建立一个目录
Directory.CreateDirectory(UploadFileDestination);
}
//保存图片
upFile.SaveAs(UploadFileDestination+strTempName+"."+AttachExtName);
}
catch
{
Response.Write("<script>window.alert('上传附件失败!')</script>");
blPicture=false;
}
}
}
}
(2) 前台的HtmlInputFile控件一定要加上runat="server" 这样后台才能获取 。
相关文章推荐
- 如何通过HtmlInputFile控件上传文件的类
- 通过HtmlInputFile控件上传文件的类
- ASP.Net之HTMLInputFile控件上传文件
- 通过HtmlInputFile控件上传文件的类
- html,图片上传预览,input file获取文件等相关操作
- HtmlInputFile的上传文件功能失效了
- HTML <input type="file">结合asp.net的一个文件上传示例
- HTML供文件上传功能浏览功能的元素是<input/>的file类型
- 通过HtmlInputFile控件上传文件的类
- html中,文件上传时使用的<input type="file">的样式自定义
- 通过HtmlInputFile控件上传文件的类
- HTML中上传与读取图片或文件(input file)----在路上(25)
- 通过HtmlInputFile控件上传文件的类
- 通过HtmlInputFile控件上传文件的类
- html input file标签的上传文件 注意点
- HTML中文件上传时使用的<input type="file">元素的样式自定义
- HTML <input type="file">上传文件——结合asp.net的一个文件上传示例
- [翻译]运用HtmlInputFile服务器控件上传多个文件
- HTML通过button触发input-file控件上传文件的问题
- 在C#.net中做页面上传的程序。用Dhtml的控件:(创建文件上载控件,该控件带有一个文本框和一个浏览按钮。)和类HtmlInputFile的两种方法