如何在asp.net中获取文件上传控件的完整路径?

时间:2011-08-26 06:55:20

标签: asp.net file-upload

我在项目中使用asp.net 2.0使用文件上传控件,所以浏览驱动器并选择文件路径(D:\ user doc new 2011 \ Montana \ MT_SUTA_2010-2011.html) 但在我的代码中看到错误是找不到文件路径 (D:\ Paymycheck \ OnlineTaxUserDocumentation-1 \ TaxesDocument \ MT_SUTA_2010-2011.html)实际上它是应用程序路径并且仅采用文件名我的代码是

if (FileUpload.HasFile)
        {
string filepath = Server.MapPath(FileUpload.FileName);
string strHTML = File.ReadAllText(filepath);
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                byte[] file1 = encoding.GetBytes(strHTML);
                int len = file1.Length;
                byte[] file = new byte[len]; 

                docs.TaxAuthorityName = ddlTaxAuthority.SelectedItem.Text;
                docs.TaxTypeID = ddlTaxType.SelectedValue;
                docs.TaxTypeDesc = ddlTaxType.SelectedItem.Text;
                docs.EffectiveDate = Convert.ToDateTime(txtEffectiveDate.Text);

                docs.FileName = f1;
                if (ddlTaxAuthority.SelectedValue == "FD")
                {

                    docs.Add(strHTML, file1);
                }
}

此行中出现错误

string strHTML = File.ReadAllText(filepath);

我也可以这样尝试

string FolderToSearch = System.IO.Directory.GetParent(FileUpload.PostedFile.FileName).ToString();
string f = Path.GetDirectoryName(FileUpload.PostedFile.FileName);
string f1 = FileUpload.FileName;
                 string filepath = System.IO.Path.GetFullPath(FileUpload.PostedFile.FileName);
 string strFilePath = FileUpload.PostedFile.FileName;
string file1234 = System.IO.Path.GetFullPath(FileUpload.PostedFile.FileName);
string filepath = FileUpload.PostedFile.FileName;

所以如何获取文件的总路径驱动器 请帮助我任何人

谢谢你 hemanth

3 个答案:

答案 0 :(得分:1)

因为您使用的是Server.MapPath,根据MSDN“maps the specified relative or virtual path to the corresponding physical directory on the server.”您必须先调用FileUpload.SaveAs方法将文件保存在服务器上,然后尝试阅读它的内容。

答案 1 :(得分:0)

试试这个会起作用

string filepath = System.IO.Path.GetFullPath(fuldExcel.PostedFile.FileName);
fuldExcel.SaveAs(filepath); //fuldExcel---is my fileupload control ID

答案 2 :(得分:-1)

如果您希望通过D:\user doc new 2011\Montana\MT_SUTA_2010-2011.html控件上传文件MT_SUTA_2010-2011.html的{​​{1}}总客户端驱动器路径,请尝试使用fileupload

这肯定会返回文件的客户端路径。

相关问题