Get real path instead of 'fakepath' in file upload

天大地大妈咪最大 提交于 2021-02-05 06:12:26

问题


I face the following problem:

When a user uploads a file with the HTML file input and I then want to receive the file path itself. I only get C:/fakepath/filename.txt for example.

I understand that it is a security reason for browsers to know the exact path of the file. So i was wondering if it is even possible with some hack, some way in .net or with additional jquery/js plugin to get the full path of the file.

Why?

We dont want to upload the file itself to our server filesystem, neither to the database. We just want to store the local path in the database so when the same user opens the site, he can click on that path and his local file system opens.

Any suggestions or recommendations for this approach?

If this is really not possible like

How to resolve the C:\fakepath?

How To Get Real Path Of A File Using Jquery

we would need to come up with a diffrent idea I guess. But since some of the answers are really old, I thought maybe there is a solution to it by now. Thx everyone


回答1:


You'll need your own code running outside browser-box to do this, since browsers are designed NOT to allow this.

I mean something ugly like ActiveX, flash, COM object, custom browser extenstion or other fancy security breach that can open it's own OpenFileDialog and insert that value in your input field.




回答2:


You can't do it.

And if you find a way, it's big security vulnerability that the browser manufacturer will fix when discovered.




回答3:


As my goal was to make the uploaded file name visible to the End User and then send it via php mail() function, All I did to resolve this was:

in your js file

Old function:

var fileuploadinit = function(){
    $('#career_resume').change(function(){
        var pathwithfilename = $('#career_resume').val();
        $('.uploadedfile').html("Uploaded File Name :" + pathwithfilename).css({
            'display':'block'
        });
    });
};

Corrected function:

var fileuploadinit = function(){
    $('#career_resume').change(function(){
        var pathwithfilename = $('#career_resume').val();
        var filename = pathwithfilename.substring(12);
        $('.uploadedfile').html("Uploaded File Name :" + filename).css({
            'display':'block'
        });
    });
};
$(document).ready(function () {
fileuploadinit();
});

Old result:

Uploaded File Name :C:\fakepath\Coverpage.pdf

New result:

Uploaded File Name :Coverpage.pdf

Hope it helps :)



来源:https://stackoverflow.com/questions/50484303/get-real-path-instead-of-fakepath-in-file-upload

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