C # multiple pictures are saved to the database in a separate upload save to the specified folder

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.