asp.net獲取html表單file中的路徑的方法

字號:


    這篇文章主要介紹了asp.net怎么獲取html表單file中的路徑,需要的朋友可以參考下
    代碼如下:
    #region 上傳文件到數(shù)據(jù)庫和服務(wù)器
    public void fn_upfiles()
    {
    //遍歷file表單元素
    httpfilecollection files = httpcontext.current.request.files;
    try
    {
    for (int ifile = 0; ifile < files.count; ifile++)
    {
    //檢查文件擴(kuò)展名字
    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 + 成功! + 服務(wù)器路徑: + 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