代码改变世界

MVC上传图片

2015-07-17 09:24  熠旸  阅读(304)  评论(0编辑  收藏  举报

 

 

<td rowspan="8"> 图片为:
<input type="hidden" name="img_url" id="img_url" value="@Model.img_url"/>
<img id="img_head1" style="height:200px; width:300px; @(Model.img_url == "" ? "display:none;" : "")" src="@(AppSettingGetValue.getFileServices() + "platformContent/TuWen/" + Model.img_url)"/><br />
<input type="file" name="fileImgUrl" id="fileToUpload1" multiple="multiple" onchange="fileSelected(1);" />
<a id="uploadFile1" style="display: none" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="uploadFile(1)">上传</a>
</td>

 

 

JQuery控制图片上传

 

function fileSelected(itype) {
$("#uploadFile" + itype).show();
}

var uploadtype = 0;
function uploadFile(itype) {
uploadtype = itype;
var fd = new FormData();
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", uploadComplete, false);
fd.append("fileToUpload", document.getElementById('fileToUpload' + itype).files[0]);
if (itype == 0) fd.append("UpLoadAdress", "platformContent\\TuWen\\");
else fd.append("UpLoadAdress", "platformContent\\TuWen\\");
xhr.open("POST", "@Url.Content("~/Common/UploadFile")");
xhr.send(fd);
}

function uploadComplete(evt) {

/* 服务器返回数据 */
var message = evt.target.responseText;
if (uploadtype == 0) {
$("#img_head").show();
$("#img_head").attr("src", "@(BBAdmin.Common.AppSettingGetValue.getFileServices())platformContent/TuWen/" + message);

if ($("#fileToUpload" + uploadtype).val() == "") {
$.message.alert("提示", "二维码上传成功!", "info");
}
else {
$("#fileToUpload" + uploadtype).val("");
}
}
if (uploadtype == 1) {
$("#img_head1").show();
$("#img_url").val(message);
$("#img_head1").attr("src", "@(BBAdmin.Common.AppSettingGetValue.getFileServices())platformContent/TuWen/" + message);
//$("#img_url").val($("#img_head1").attr("src"));

if ($("#fileToUpload1" + uploadtype).val() == "") {
$.messager.alert("提示", "头像上传成功!", "info");
}
else {
$("#fileToUpload1" + uploadtype).val("");
}
}
}