ASP. NET Image Upload (with jquery. from. js plug-in ),

Source: Internet
Author: User

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 ";

}

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.