Ruan
Ruan

Reputation: 4283

How to use a file from a client in ASP.net?

I know the server cant access the hard drive or directories of a client, but why when I Hardcode the directory into my application can I access the file and upload it to a SQL database?

Just a plain button

 <asp:LinkButton ID="btnImportData" runat="server" OnClick="btnImportData_Click">Import Spreadsheet</asp:LinkButton>

then the Code behind

  protected void btnImportData_Click(object sender, EventArgs e)
        {
            dbConn.uploadToExcel(@"C:\Users\spadmin\Desktop\ABI Employee List.xlsx");
        }

Then I can use that file to update my database and it works.

Why do they then say I cant use directories from the client?

UPDATE

Link on how to upload to server using Server.Mappath

Upvotes: 0

Views: 1431

Answers (2)

Sen Jacob
Sen Jacob

Reputation: 3442

You should not hardcode links like that. Allow the user to pick up the file for uploading using FileUpload control.

The path you gave points to your local account's desktop only (Your local server). Actually it is not uploading the file from client's machine.

When you save files to server, use Server.MapPath().

Upvotes: 0

gareththegeek
gareththegeek

Reputation: 2418

The code accesses the file on the server. Regardless of which PC you use to access the web server it is always looking on the server's C drive.

Upvotes: 3

Related Questions