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

问题描述:

我正在使用文件上传控件在我的项目中使用asp.net 2.0,因此浏览驱动器并选择文件,例如path(D:\ user doc new 2011 \ Montana \ MT_SUTA_2010-2011.html)强> 但是在我的代码中看到错误是找不到文件路径是 (D:\ Paymycheck \ OnlineTaxUserDocumentation-1 \ TaxesDocument \ MT_SUTA_2010-2011.2011)实际上是应用程序路径,仅使用我的代码获取文件名

i am using asp.net 2.0 in my project using file upload control , so browse the drive and select the file like path(D:\user doc new 2011\Montana\MT_SUTA_2010-2011.html) but in my code seeing error is could not find the file path is (D:\Paymycheck\OnlineTaxUserDocumentation-1\TaxesDocument\MT_SUTA_2010-2011.html) actually it is application path and take the filename only my code is

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;

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

so how to get the total path drive to file pls help me any one

谢谢你 生命力

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

Because your are using Server.MapPath, which according to MSDN "maps the specified relative or virtual path to the corresponding physical directory on the server." You have to first call FileUpload.SaveAs method to save file on server then try to read it's content.