1. Reference JS file
<script src= "~/scripts/uploadpreview.js" ></script>
Tocalid () non-null validation before form submission
<form action= "/home/insertimg" method= "post" enctype= "Multipart/form-data" onsubmit= "return Tocalid ()" >
<ul>
<li>
<input type= "File" id= "Up_img_wu_file_0" Name= "P1"/>
</li>
<li>
<input type= "File" id= "Up_img_wu_file_1" Name= "P1"/>
</li>
</ul>
</form>
[post]
Public ActionResult insertimg () {
Get picture information
HttpPostedFileBase file = request.files["P1"];
String Filename= "";
String ensrc1 = "";
Determine if there is a picture
if (file.length>1) {
Get file name
filename = path.getfilename (file. FileName);
string privous = filename. Substring (0, filename. LastIndexOf ('. '));
String next = filename. Substring (filename. LastIndexOf ('. '));
Storage location
string filepath = Path.Combine (Server.MapPath ("~/enclosures/"), filename);
Store
File. SaveAs (filepath);
Stored path port + file location + filename
ENSRC1 = "http:/" + Request.UrlReferrer.Authority + "/enclosures/" + filename;
}
Call the BLL layer method and put the parameters in
}
JS file:
var Uploadpreview = function (setting) {
var _self = this;
_self. IsNull = function (value) {
if (typeof (value) = = "function") {return false;}
if (value = = Undefined | | value = = NULL | | value = = "" "| | Value.length = = 0) {
return true;
}
return false;
}
_self. Defautlsetting = {
UPBTN: "",
UPBTN: "",
Divshow: "",
Imgshow: "",
WIDTH:100,
HEIGHT:100,
Imgtype: ["gif", "JPEG", "JPG", "BMP", "PNG"],
ErrMsg: "Select File error, picture type must be one of (gif,jpeg,jpg,bmp,png)",
Callback:function () {}
};
_self. Setting = {
Uploader: '/commodity/fileimageupdateoradd ',
Upbtn: _self. IsNull (setting. UPBTN)? _self. DefautlSetting.UpBtn:setting. UPBTN,
Divshow: _self. IsNull (setting. divshow)? _self. DefautlSetting.DivShow:setting. Divshow,
Imgshow: _self. IsNull (setting. imgshow)? _self. DefautlSetting.ImgShow:setting. Imgshow,
Width: _self. IsNull (setting. Width)? _self. DefautlSetting.Width:setting. Width,
Height: _self. IsNull (setting. Height)? _self. DefautlSetting.Height:setting. Height,
Imgtype: _self. IsNull (setting. Imgtype)? _self. DefautlSetting.ImgType:setting. Imgtype,
ErrMsg: _self. IsNull (setting. errmsg)? _self. DefautlSetting.ErrMsg:setting. ErrMsg,
Callback: _self. IsNull (setting.callback)? _self. DefautlSetting.callback:setting.callback
};
_self.getobjecturl = function (file) {
var url = null;
if (window.createobjecturl! = undefined) {
url = window.createobjecturl (file);
} else if (window. URL! = undefined) {
url = window. Url.createobjecturl (file);
} else if (window.webkiturl! = undefined) {
url = window.webkitURL.createObjectURL (file);
}
return URL;
}
_self. Bind = function () {
document.getElementById (_self. SETTING.UPBTN). onchange = function () {
if (this.value) {
if (! REGEXP ("\. (" + _self. Setting.ImgType.join ("|") + ") $", "I"). Test (This.value.toLowerCase ())) {
Alert (_self. SETTING.ERRMSG);
This.value = "";
return false;
}
if (Navigator.userAgent.indexOf ("MSIE") >-1) {
try {
document.getElementById (_self. setting.imgshow). src = _self.getobjecturl (this.files[0]);
} catch (e) {
var div = document.getElementById (_self. Setting.divshow);
This.select ();
Top.parent.document.body.focus ();
var src = document.selection.createRange (). text;
Document.selection.empty ();
document.getElementById (_self. setting.imgshow). style.display = "None";
Div.style.filter = "Progid:DXImageTransform.Microsoft.AlphaImageLoader (Sizingmethod=scale)";
Div.style.width = _self. Setting.width + "px";
Div.style.height = _self. Setting.height + "px";
Div.filters.item ("DXImageTransform.Microsoft.AlphaImageLoader"). src = src;
}
} else {
document.getElementById (_self. setting.imgshow). src = _self.getobjecturl (this.files[0]);
}
_self. Setting.callback ();
}
}
}
_self. Bind ();
}
function File_click () {
var WARP = document.getElementById (' WARP ');
var warp_li = warp.getelementsbytagname (' LI ');
for (var i=0; i<warp_li.length;i++) {
New Uploadpreview ({upbtn: "Up_img_wu_file_" +i, Imgshow: "Imgshow_wu_file_" +i});
}
}
Window.onload = File_click;
C # multiple pictures are saved to the database in a separate upload save to the specified folder