How to resolve the C:\fakepath?

后端 未结 13 1694
梦如初夏
梦如初夏 2020-11-21 13:34

This is my upload button.



        
相关标签:
13条回答
  • 2020-11-21 13:56

    Complementing Sardesh Sharma's answer use:

    document.getElementById("file-id").files[0].path
    

    For full path.

    0 讨论(0)
  • 2020-11-21 14:01

    Use

    document.getElementById("file-id").files[0].name; 
    

    instead of

    document.getElementById('file-id').value
    
    0 讨论(0)
  • 2020-11-21 14:04

    seems you can't find the full path in you localhost by js, but you can hide the fakepath to just show the file name. Use jQuery to get the file input's selected filename without the path

    0 讨论(0)
  • 2020-11-21 14:05

    I am happy that browsers care to save us from intrusive scripts and the like. I am not happy with IE putting something into the browser that makes a simple style-fix look like a hack-attack!

    I've used a < span > to represent the file-input so that I could apply appropriate styling to the < div > instead of the < input > (once again, because of IE). Now due to this IE want's to show the User a path with a value that's just guaranteed to put them on guard and in the very least apprehensive (if not totally scare them off?!)... MORE IE-CRAP!

    Anyhow, thanks to to those who posted the explanation here: IE Browser Security: Appending "fakepath" to file path in input[type="file"], I've put together a minor fixer-upper...

    The code below does two things - it fixes a lte IE8 bug where the onChange event doesn't fire until the upload field's onBlur and it updates an element with a cleaned filepath that won't scare the User.

    // self-calling lambda to for jQuery shorthand "$" namespace
    (function($){
        // document onReady wrapper
        $().ready(function(){
            // check for the nefarious IE
            if($.browser.msie) {
                // capture the file input fields
                var fileInput = $('input[type="file"]');
                // add presentational <span> tags "underneath" all file input fields for styling
                fileInput.after(
                    $(document.createElement('span')).addClass('file-underlay')
                );
                // bind onClick to get the file-path and update the style <div>
                fileInput.click(function(){
                    // need to capture $(this) because setTimeout() is on the
                    // Window keyword 'this' changes context in it
                    var fileContext = $(this);
                    // capture the timer as well as set setTimeout()
                    // we use setTimeout() because IE pauses timers when a file dialog opens
                    // in this manner we give ourselves a "pseudo-onChange" handler
                    var ieBugTimeout = setTimeout(function(){
                        // set vars
                        var filePath     = fileContext.val(),
                            fileUnderlay = fileContext.siblings('.file-underlay');
                        // check for IE's lovely security speil
                        if(filePath.match(/fakepath/)) {
                            // update the file-path text using case-insensitive regex
                            filePath = filePath.replace(/C:\\fakepath\\/i, '');
                        }
                        // update the text in the file-underlay <span>
                        fileUnderlay.text(filePath);
                        // clear the timer var
                        clearTimeout(ieBugTimeout);
                    }, 10);
                });
            }
        });
    })(jQuery);
    
    0 讨论(0)
  • 2020-11-21 14:05

    Use file readers:

    $(document).ready(function() {
            $("#input-file").change(function() {
                var length = this.files.length;
                if (!length) {
                    return false;
                }
                useImage(this);
            });
        });
    
        // Creating the function
        function useImage(img) {
            var file = img.files[0];
            var imagefile = file.type;
            var match = ["image/jpeg", "image/png", "image/jpg"];
            if (!((imagefile == match[0]) || (imagefile == match[1]) || (imagefile == match[2]))) {
                alert("Invalid File Extension");
            } else {
                var reader = new FileReader();
                reader.onload = imageIsLoaded;
                reader.readAsDataURL(img.files[0]);
            }
    
            function imageIsLoaded(e) {
                $('div.withBckImage').css({ 'background-image': "url(" + e.target.result + ")" });
    
            }
        }
    
    0 讨论(0)
  • 2020-11-21 14:07

    Hy there , in my case i am using asp.net development environment, so i was want to upload those data in asynchronus ajax request , in [webMethod] you can not catch the file uploader since it is not static element , so i had to make a turnover for such solution by fixing the path , than convert the wanted image into bytes to save it in DB .

    Here is my javascript function , hope it helps you:

    function FixPath(Path)
             {
                 var HiddenPath = Path.toString();
                 alert(HiddenPath.indexOf("FakePath"));
    
                 if (HiddenPath.indexOf("FakePath") > 1)
                 {
                     var UnwantedLength = HiddenPath.indexOf("FakePath") + 7;
                     MainStringLength = HiddenPath.length - UnwantedLength;
                     var thisArray =[];
                     var i = 0;
                     var FinalString= "";
                     while (i < MainStringLength)
                     {
                         thisArray[i] = HiddenPath[UnwantedLength + i + 1];
                         i++;
                     }
                     var j = 0;
                     while (j < MainStringLength-1)
                     {
                         if (thisArray[j] != ",")
                         {
                             FinalString += thisArray[j];
                         }
                         j++;
                     }
                     FinalString = "~" + FinalString;
                     alert(FinalString);
                     return FinalString;
                 }
                 else
                 {
                     return HiddenPath;
                 }
             }
    

    here only for testing :

     $(document).ready(function () {
                 FixPath("hakounaMatata:/7ekmaTa3mahaLaziz/FakePath/EnsaLmadiLiYghiz");
             });
    // this will give you : ~/EnsaLmadiLiYghiz
    
    0 讨论(0)
提交回复
热议问题