标题 | asp.net获取html表单file中的路径的方法 |
范文 | 这篇文章主要介绍了asp.net怎么获取html表单file中的路径,需要的朋友可以参考下 代码如下: #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 |
随便看 |
|
在线学习网范文大全提供好词好句、学习总结、工作总结、演讲稿等写作素材及范文模板,是学习及工作的有利工具。