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

懵懂的女人 提交于 2019-12-12 02:54:02

问题


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


回答1:


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.




回答2:


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().



来源:https://stackoverflow.com/questions/16355855/how-to-use-a-file-from-a-client-in-asp-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!