简体   繁体   中英

Retrieving full local path in file upload control in ASP.NET

Is it possible to get access to the local path of file in file upload control?

Eg I have a file C:\\DummyData\\Hello.txt

ASP.NET Project is stored here C:\\Project\\FileUploadTest which has a default.aspx page containing file upload control say fileUpload1.

In code behind, I use

string filename = Path.GetFullPath(fileUpload1.PostedFile.FileName);

but this gives me filename as

 C:\\Program Files\\Common Files\\Microsoft Shared\\DevServer\\10.0\\Hello.txt

Is there a way I can capture C:\\DummyData\\Hello.txt?

I can assume that because of security reason, the browsers cannot show the actual path and hence show fakepath.. But can't this be accessed in code behind?

I can assume that because of security reason, the browsers cannot show the actual path and hence show fakepath.. But can't this be accessed in code behind?

You are correct. It can't be accessed in code, either, partly due to security reasons but also, and probably more importantly, why would you need it? You couldn't do anything with that path if you could get it on the server. Other than when you're developing the server and client machines are two different machines.

As stated above, it doesn't do it, but you could try a workaround using JavaScript and a hidden input. I haven't tested the code, but something like this would do it:

HTML

<asp:FileUpload ID="fileUpload1" runat="server" />
<input type="hidden" id="hidLocalPath" runat="server" />

JavaScript (using jQuery)

$('#<%=fileUpload1.ClientID %>').change(function() {
    $('#<%=hidLocalPath.ClientID %>').val($(this).val());
});

Server Side (C#)

string localFileName = hidLocalPath.Value;

Obviously you'd want to do validation but I have left that out for brevity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM