Chromeアップロード画像パスc:/ fakepathソリューション



Chrome Upload Image Path C



ブラウザはセキュリティレベルを改善したため、デフォルトではファイルの実際のパスを取得せず、偽のパスに置き換えられます。

1.ブラウザのセキュリティレベルは次の方法で変更できます。[ツール]-> [インターネットオプション]-> [セキュリティ]-> [カスタムレベル]-> [検索] [その他]の[サーバーにローカルファイルをアップロードするときにローカルディレクトリパスを含める]を選択し、[有効にする]を選択します。結構です。
2.上記の方法は、プログラマーにとっては絶対に望ましくありません。すべてのユーザーがブラウザの構成を変更することは許可されていません。 jsコードでコードを変更する方法はありますが、IEとのみ互換性があります。 Firefoxシリーズ。



function getPath(){ var Pathobj = document.getElementById('......') if(Pathobj){ if (window.navigator.userAgent.indexOf('MSIE')>=1) { Pathobj.select() return document.selection.createRange().text }else if(window.navigator.userAgent.indexOf('Firefox')>=1){ if(Pathobj.files){ return Pathobj.files.item(0).getAsDataURL() } return Pathobj.value } return Pathobj.value } }

私の考えは:

非表示のタグをページに追加すると、jsはURLを非表示のタグに渡し、最後にAjaxが変更しますdata.field.file投稿の値を変更して投稿します。
//Used to receive the value of the correct url data.field.file = document.getElementById('img_url').value

完全なコード:

// Upload image processing var uploadInst = upload.render({ Elem: '#upload_img' // binding element , url: '..' // upload interface ,size:50 , accept: 'images' //Specify the file type that is allowed to be verified at upload time , acceptMime: 'image/*'//Filtered file types , before: function(obj){ //callback after selecting the file obj.preview(function(index, file, result){ $('#pre_img').attr('src', result) //Image link (base64), inserted for preview }) } ,done: function(res){ / / Upload completed callback if(res.code >0){ Return layer.msg('Image upload failed') } document.getElementById('img_url').value = res.msg } ,error: function(){ / / Request an exception callback layer.closeAll('loading') //close loading } }) / / Monitor the submission form.on('submit(add)', function(data){ data.field.file = document.getElementById('img_url').value / / Send asynchronous, submit data to php $.ajax({ url:'./goods_add_function.php', data:data.field, dataType:'text', type:'post', success:function (data) { Layer.alert('add success', {icon: 6}, function () { / / Get the frame index var index = parent.layer.getFrameIndex(window.name) / / Close the current frame parent.layer.close(index) }) } }) return false })