ASP. NET Image Upload (with jquery. from. js plug-in ),
Front end:
Js:
Function AjaxKouBeiShopEdit (){
Var options = {
DataType: "json ",
Success: function (data ){
Alert (data );
},
Error: function (XMLHttpRequest, textStatus, errorThrown ){
Console. log (textStatus );
Console. log (errorThrown );
}
};
$ ("# ShopForm"). ajaxSubmit (options );
}
Html:
<Form class = "form-horizontal" id = "ShopForm" action = "your Controller/AjaxKouBeiShopEdit" method = "post" enctype = "multipart/form-data">
<Input type = "file" name = "ProductImg" class = "fileData"/>
<Input type = "file" name = "ProductImg" class = "fileData"/>
</Form>
Backend:
/// <Summary>
/// Koubei store-add
/// </Summary>
/// <Param name = "upImg"> </param>
/// <Param name = "entity"> </param>
/// <Returns> </returns>
[HttpPost]
Public string AjaxKouBeiShopEdit (koubeisw.model entity)
{
StringBuilder sqlStr = new StringBuilder ();
TimeSpan timeSpan = (DateTime. Now-TimeZone. CurrentTimeZone. ToLocalTime (new System. DateTime (1970, 1, 1 )));
Random random = new Random ();
JObject resultJsonerr = new JObject ();
// Modify the operation
# Add a file to region
String filePhysicalPath = @ "D:/yyyy /";
String MainImgName = "";
// Detailed image path
If (Request. Files. Count! = 0)
{
MainImgName = filePhysicalPath + MainImgName;
Try
{
For (int I = 0; I <Request. Files. Count; I ++)
{
HttpPostedFileBase uploadFile = Request. Files [I] as HttpPostedFileBase;
If (uploadFile! = Null & uploadFile. ContentLength> 0)
{
// If (I = 0) // first graph
//{
// Loading html files to the bottom, file input
//}
// Random name
Long t2 = (long) timeSpan. TotalSeconds;
Int n2 = random. Next (10000,999 99 );
String extension = Path. GetExtension (uploadFile. FileName );
String ImgListName = t2 + "" + n2 + extension;
Var path = Path. Combine (filePhysicalPath, ImgListName );
UploadFile. SaveAs (path );
}
}
}
Catch (Exception ex)
{
ResultJsonerr. Add ("State",-1 );
Return "failed to add ";
}
}
Return "added successfully ";
}