0

We have an issue tracking system on our intranet where we need to add effected files to the issue, including the path of the file.

We have been using an asp.net FileUpload control and using FileUpload1.PostedFile.FileName to get the path of the file that the user selects and uploads. The files are usually not on the users local machine, but on network drives that are on a separate server than the issue tracking system. We use the upload as an easy way to add files to the issue tracker.

This does not work in any recent browsers for security reasons, only on Internet Explorer which is on its way out.

Is there any way to get this to work? At least for intranet sites? Or is there a better way to go about this? We do absolutely need the file path, and if we had to copy and paste it would make this process take much longer as it would make it a two step process and many files often need to be added.

Here is a snippet of the asp.net page:

<asp:FileUpload ID="FileUpload1" runat="server" />

And here is how I was getting the file path:

Dim sFileLocation As String = ""
Dim sFileName As String = ""
Dim sExtension As String = ""

sFileName = GetFileName(Me.FileUpload1.FileName)
sExtension = GetExtension(Me.FileUpload1.FileName)
sFileLocation = Me.FileUpload1.PostedFile.FileName
sFileLocation = sFileLocation.Replace(sFileName, "")
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
rgorr
  • 331
  • 3
  • 18
  • 1
    What is it you are trying to do? You aren't able to capture file paths of users that upload files? Please clarify and perhaps add a snippet of your code so we can get a better picture. – JobesK Aug 25 '20 at 17:02
  • @JobesK I am trying to get the full directory of the uploaded file on the users machine. It is then stored in the database and referenced in our issue tracking system. – rgorr Aug 25 '20 at 17:09
  • 2
    This is not possible, as it would be a security issue to get this information of the client machine from the server machine. – Uwe Keim Aug 25 '20 at 17:12
  • @UweKeim This is from an intranet site so it should not be a security issue in this case, which is why I am looking for a workaround. Whether it involves browser plugins or a different method. – rgorr Aug 25 '20 at 17:15
  • Not possible IMO. Even the [client-side JS File API](https://developer.mozilla.org/de/docs/Web/API/File) does not provide a way to get the local folder. Probably duplicate of [this](https://stackoverflow.com/q/1130560/107625). Are you trying to solve an [XY problem](http://xyproblem.info/)? Maybe there are browser-specific hacks. – Uwe Keim Aug 25 '20 at 17:16
  • @UweKeim as I said, I am open to other solutions to my problem. I do absolutely need the file paths. – rgorr Aug 25 '20 at 17:19
  • Try to use java script to get the path from the file upload control and save it into the hidden field, after you click post back button, then get the path info from the hidden field – Tony Dong Aug 25 '20 at 17:19
  • @TonyDong Even with JS this [doesn't seem to be possible](https://developer.mozilla.org/en/docs/Web/API/File). Or are you refering to another solution? Any links/documentation? – Uwe Keim Aug 25 '20 at 17:21
  • It is works for me to get file path by java script var filePath=$('<%=FileUpload1.ClientID%>').val(); I can use the path to check the file size, name before upload to the server – Tony Dong Aug 25 '20 at 17:29
  • Does this answer your question? [Get full path of a file with FileUpload Control](https://stackoverflow.com/questions/1130560/get-full-path-of-a-file-with-fileupload-control) – Heretic Monkey Aug 25 '20 at 17:53
  • You could probably create a browser extension or otherwise run sort of code that executes outside of the browser's sandbox. There won't be a simple solution to get this working through normal browser techniques - anything you managed to find would be a security vulnerability and would likely get patched up. – mason Aug 25 '20 at 18:02

2 Answers2

0

This is only works for IE 11, did not test IE edge. Chrome does not work. Due to intranet, we only allow use IE internally.

Here is the full path of the file came from in IE 11

Here is the logic I am using to check the file size and name, validateFile function can get the file path without problem

            <asp:FileUpload ID="fuAFS" runat="server" CssClass="cssAFS cssFu" AllowMultiple="false" Width="550px" />&nbsp;<asp:Button ID="btnAFS" runat="server" Text="Upload AFS" OnClientClick="return ValidateFile('cssAFS');" />



           function ValidateFile(cssName) {
                var fu = $('.' + cssName);
                //Empty upload control file.length == 1
                if (fu.length > 0) { //Upload control exists
                    var pathName = fu.val();
                    if (pathName == '') {
                        alert('Missing file. Please click the Browse button to select a file with extension pdf.');
                        return false;
                    }
                }
                return true;
            }
            $('.cssFu').on('change', function (e) {
                var maxFileSize = $('#<%=hdnMaxFileSize.ClientID%>').val();
            if (e.target.files.length == 1) {
                if (e.target.files[0] != null) {
                    var size = e.target.files[0].size; //IE 
                    var fileSize = e.target.files[0].fileSize; //Firefox
                    var pathName = $(this).val();
                    var ext = pathName.substring(pathName.lastIndexOf(".") + 1, pathName.length).toLowerCase();
                    if (size > maxFileSize * 1024 * 1024 || fileSize > maxFileSize * 1024 * 1024) {
                        alert('File size exceeds limit. Maximum file size permitted is ' + maxFileSize + ' MB.');
                        $(this).val('');
                        // Prevent form Upload
                        e.stopPropagation();
                        e.preventDefault();
                    } else if (ext != 'pdf') {
                        alert('Invalid File. Please upload a File with extension: PDF');
                        $(this).val('');
                        // Prevent form Upload
                        e.stopPropagation();
                        e.preventDefault();
                    }
                    var fileName = pathName.substring(pathName.lastIndexOf("\\") + 1, pathName.length);
                    if (fileName != '' && fileName.length > 100) {
                        alert('File name length over 100 characters, please rename the file and upload again.');
                        $(this).val('');
                        // Prevent form Upload
                        e.stopPropagation();
                        e.preventDefault();
                    }
                }
            }
Tony Dong
  • 3,213
  • 1
  • 29
  • 32
0

@rgorr - FileUpload will never give you the full path for security reasons.

Server.MapPath(FileUpload1.FileName);
Purvesh Sangani
  • 295
  • 1
  • 9