1

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

hmk
  • 969
  • 10
  • 38
  • 70

3 Answers3

1

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.

Waqas
  • 6,812
  • 2
  • 33
  • 50
  • hi sir pls give me example i think string s=fileupload.saveas(filename); – hmk Aug 26 '11 at 07:09
  • i can use string sq =FileUpload.SaveAs(filepath); but display the error – hmk Aug 26 '11 at 07:16
  • It has no return type, try FileUpload.SaveAs(filePath) without sq = – Waqas Aug 26 '11 at 07:19
  • ok sir it is working but how to read the content like string strHTML = File.ReadAllText(filepath); – hmk Aug 26 '11 at 07:24
  • File.ReadAllText(filepath); isnt this working? what is the error you are reveing? – Waqas Aug 26 '11 at 07:44
  • it is working string pathq = Server.MapPath("~/"); FileUpload.SaveAs(pathq + FileUpload.FileName); string path1 = pathq + FileUpload.FileName; string strHTML = File.ReadAllText(path1); – hmk Aug 26 '11 at 08:05
-1

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.

Unai Vivi
  • 3,073
  • 3
  • 30
  • 46
Shashi
  • 1
-1

Try this it will work

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