-1

I want to create a document matrix that allows users to upload files into a web based database. I am getting errors regrading to the access the file path. System.IO.FileNotFoundException: 'Could not find file 'C:\Program Files (x86)\IIS Express\auto.docx'.'

The data type in the database is Varbinary(MAX). Also there is no issue with the connection to the database.

Here is the button event for uploading the file.

    protected void Button1_Click(object sender, EventArgs e)
    {
        //get file path from file upload button 
         String filepath=    System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName);

        //Converts file(.txt pdf etc..) into byte[]
        byte[] filedata = System.IO.File.ReadAllBytes(filepath);

       //Insert file into database
        cmd.CommandText = "INSERT INTO tbl_test (Name)" +
                                    " Values ('" + TextBox1.Text + "','" + filedata + "')";
        cmd.Connection = mycon;
        cmd.ExecuteNonQuery();
        mycon.Close();
    }
6
  • Just to add. this is local on my machine for now. I was thinking there might be security issues with accesses files. Commented Mar 15, 2018 at 11:36
  • 1
    System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName); makes no sense. The file isn't on the file system. It is in memory. Commented Mar 15, 2018 at 11:38
  • Have a read of msdn.microsoft.com/en-us/library/… . Commented Mar 15, 2018 at 11:40
  • if that Path is wrong, what is the correct way Commented Mar 15, 2018 at 11:52
  • but i'm using the fileupload button. which allows me to browse and select a file. Surly by me selecting the file, the path can be found. Therefore a path can be generated. Commented Mar 15, 2018 at 12:19

1 Answer 1

-2

Are you by any chance using Windows 10?

If so, is that folder a 'read-only' folder? I had this issue last year and could check for you to confirm how I fixed it.

if (FileUpload1.PostedFile != null)
    {
        filePath = FileUpload1.PostedFile.FileName; // gets the file name with path.
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Yes im on windows 10. Thank you
It is irrelevant. The file isn't on the file system.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.