how to get the full path of file upload control in

2019-08-30 05:53发布

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);
                }
}

error occoured in this line

string strHTML = File.ReadAllText(filepath);

i can try like this also

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

thank u hemanth

3条回答
▲ chillily
2楼-- · 2019-08-30 06:19

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.

查看更多
男人必须洒脱
3楼-- · 2019-08-30 06:26

If you want the total client side drive path like D:\user doc new 2011\Montana\MT_SUTA_2010-2011.html for the file MT_SUTA_2010-2011.html to be uploaded via fileupload control, then try using System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName).

This will surely return the client side path of the file.

查看更多
时光不老,我们不散
4楼-- · 2019-08-30 06:27

Try this it will work

string filepath = System.IO.Path.GetFullPath(fuldExcel.PostedFile.FileName);
fuldExcel.SaveAs(filepath); //fuldExcel---is my fileupload control ID
查看更多
登录 后发表回答