前台頁面 上傳預覽 調用上傳服務

來源:互聯網
上載者:User

標籤:ext   偽造   window   end   cap   gid   cape   style   uri   

function openBrowse(){
var ie=navigator.appName=="Microsoft Internet Explorer" ? true : false;
if(ie){
document.getElementById("file").click();
document.getElementById("filename").value=document.getElementById("file").value;
}else{
var a=document.createEvent("MouseEvents");
a.initEvent("click", true, true);
document.getElementById("file").dispatchEvent(a);
}
}

function setImagePreview() {
if($(‘div[id="localImag"]‘).length>5){
alert("最多隻能上傳5張圖片!");
return;
}
var uploadSrc = "";
$.ajaxFileUpload({
async : false,
url : "${ctx}/xxxx/xxx/xxx",
secureuri : false,
fileElementId :[‘file‘],
dataType : ‘text‘,
success : function(data) {
uploadSrc = $(data).text();
if(uploadSrc == ‘‘ || uploadSrc.indexOf("錯誤") > -1){
$(‘#pic‘).show();
$(‘#nameImage‘).val(‘‘);
$(‘#preview‘).attr(‘src‘,uploadSrc);
$("#preview").css(‘display‘,‘none‘);
$("#localImag").css(‘display‘,‘none‘);

$(‘#cancelButton‘).hide();
return true;
}else{
$(‘#pic‘).hide();
$("#preview").css(‘display‘,‘block‘);
$("#localImag").css(‘display‘,‘block‘);
$(‘#cancelButton‘).show();
}

var docObj = document.getElementById("file");
var imgObjPreview = document.getElementById("preview");
if (docObj.files && docObj.files[0]) {
//Firefox下,直接設img屬性
imgObjPreview.style.display = ‘block‘;
imgObjPreview.style.width = ‘200px‘;
imgObjPreview.style.height = ‘150px‘;
//imgObjPreview.src = docObj.files[0].getAsDataURL();
//Firefox7以上版本不能用上面的getAsDataURL()方式擷取,需要一下方式
imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);

} else {

//IE下,使用濾鏡
docObj.select();
var imgSrc = uploadSrc;
var localImagId = document.getElementById("localImag");
//必須設定初始大小
localImagId.style.width = "200px";
localImagId.style.height = "150px";

//圖片異常的捕捉,防止使用者修改尾碼來偽造圖片
try {

$(localImagId).find(‘#preview‘).attr(‘src‘,imgSrc);
$(‘p[name="notShow"]‘).hide();
$(‘p‘).append(localImagId);
$(localImagId).append(‘<input type="hidden" name="picUrl" id="picUrl"/>‘);
$(localImagId).find(‘#picUrl‘).attr(‘value‘,imgSrc);
$(‘input[id="nameImage"]‘).attr(‘value‘,‘‘);
$("#preview").css(‘display‘,‘block‘);
} catch (e) {
alert("您上傳的圖片格式不正確,請重新選擇!");
return false;
}
}

$("#nameImage").val(uploadSrc);
$(‘#nameImage‘).attr("readonly","readonly")//將input元素設定為readonly
},
error : function(data) {
alert(data);
}
});
return true;
}

================================================

<input type=file name="fileUpload" id="file" onchange="javascript:setImagePreview(this);" style="display:none">

<li><label>獎&nbsp;品&nbsp;&nbsp;圖:</label>
<form:input id="nameImage" name="nameImage" path="picUrl" htmlEscape="false" maxlength="500" readonly="true" class="required" />
<label id="pic" for="pic" class="error" style="diplay:none">圖片類型必須是.gif,jpeg,jpg,bmp中的一種</label>
<input type="button" name="button" style="margin-bottom: 0px;" onclick="javascript:openBrowse(this);" value="上傳圖片"/>
<!-- <input type="button" id="cancelButton" name="button" style="margin-bottom: 0px;diplay:none" onclick="javascript:cancelBrowse();" value="取消上傳"/> -->
<span class="help-inline"><font color="red">*</font> </span>
<p style="diplay:none" name="notShow"><div id="localImag" class="localImags"><div class=‘clo‘ onClick=‘javascript:checkClose(this);‘></div><img id="preview" style="diplay:none" /></div></p></li>

前台頁面 上傳預覽 調用上傳服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.