您的位置:首页 > Web前端 > HTML

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" 这样后台才能获取 。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息