深Shenzhenで作業するときは、Avatarプレビュー関数をアップロードするためにユーザーが必要です!私はそれらの多くをオンラインで探しましたが、私はあまり満足していませんでした。フラッシュであるか、Ajaxのアップロード後に画像パスを返すか、まったく使用できません。幸いなことに、誰かが以前にこのプロジェクトで写真プレビュー機能を書いたので、将来や困っている他の友人のためにそれを使いやすくするために、ここでレコードを作成するためにそれを取り出しました!
次のように、コードは単純です。
< xmlns = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <title> setimagePreview(avalue){var docobj = document.getElementById( "doc"); var imgobjpreview = document.getElementById( "preview"); if(docobj.files && docobj.files [0]){// firefoxを直接設定します。 'block'; imgobjpreview.style.width = '150px'; imgobjpreview.style.height = '180px'; //imgobjpreview.src = docobj.files [0] .getasdataurl(); // firefox 7以降のバージョンは、上記のgetasdataurl()メソッドを使用して取得できません。次の方法が必要ですimgobjpreview.src = window.url.createobjecturl(docobj.files [0]);} else {// filter docobj.select(); var imgsrc = document.selection.createrange()。 localimagid.style.width = "150px"; localimagid.style.height = "180px"; //画像の例外のキャプチャは、ユーザーが接尾辞を変更して画像を偽造するのを防ぎます{localimagid.style.filter = "progid:dimbyagetransform.microsoft.alphaimageloader(sizingmethod = scale)"; localimagid.filters.item( "dmimagetransform.microsoft.alphaimageloader")。src= imgsrc;間違って、もう一度選択してください! "); return false;} imgobjpreview.style.display = 'none'; document.selection.empty();} return true;} </script> </head> <body> id = "preview" src = "http://blog.chuangling.net/public/images/top.jpg" style = "display:block;幅:150px;高さ:180px; "> </div> </td> </tr> <tr> <td align =" center "style =" padding-top:10px; "> <入力タイプ=" file "name =" file "id =" doc "onchange =" javascript:javascript:setimagepreview();テストは、IE8、FF12.0、Google Chrome 28.0.1500.72で使用できます!