-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();
    }
  • Just to add. this is local on my machine for now. I was thinking there might be security issues with accesses files. – Thomas Foran Mar 15 '18 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. – mjwills Mar 15 '18 at 11:38
  • Have a read of https://msdn.microsoft.com/en-us/library/system.web.httppostedfile.inputstream(v=vs.110).aspx . – mjwills Mar 15 '18 at 11:40
  • if that Path is wrong, what is the correct way – Thomas Foran Mar 15 '18 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. – Thomas Foran Mar 15 '18 at 12:19
  • I don't know how else to say it but your understanding of how web servers work is incorrect. It has no knowledge of your network. It doesn't care. It is simply a collection of bytes that were uploaded from a web browser and are now stored in the web server's memory. You might wish that wasn't the case, but it is indeed how the web works. – mjwills Mar 15 '18 at 12:26

1 Answers1

-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.
    }