MVC4.0 asynchronously Loads files and mvc4.0 asynchronously Loads files

Source: Internet
Author: User

MVC4.0 asynchronously Loads files and mvc4.0 asynchronously Loads files
Zookeeper

Upload Avatar Asynchronously

Front-end:

<Form id = "FileForm">
<Span style = "width: 100px;">


</Span>
<Span id = "uploadImg" style = "margin-left: 10px; width: 100px; height: 100px;">
<Input type = "file" name = "file" onchange = "previewImage ()" id = "file" value = "upload materials"/>
</Span>
</Form>


Js:
Function previewImage (){
$ ("# Oldimghead"). hide ();
$ ("# Imghead"). show ();
$ ("# FileForm"). ajaxSubmit ({
Success: function (url ){
ImageUrl = "/FileServer/UserPhoto" + url;
If (url! = "" & Url! = Null ){
$ ("# Imghead"). attr ("src", ImageUrl );
}
Else
$ ("# Imghead"). attr ("src ","~ /Content/images/HeadPortraitMan.jpg ");
},
Error: function (error) {alert (error );},
Url: '/Member/UploadPhoto',/* sets the page to which post is submitted */
Type: "post",/* set the form to be submitted using the post method */
DataType: "json"/* set the return value type to text */
});
}

Background:
// Upload the Avatar
Public ActionResult UploadPhoto ()
{
// Var LoginModel = Session ["UserLogin"]! = Null? Session ["UserLogin"] as LoginModel: null;
// Int CusACCPInfoID = LoginModel. CusACCPInfo_ID; // customer contact ID
Int CusACCPInfoID = 20;
HttpPostedFileBase hp = Request. Files ["file"];
String uploadPath = Server. MapPath ("/FileServer/UserPhoto ");
Var msg = UploadHelper. UploadImage (CusACCPInfoID, 1, hp, uploadPath );
Return Json (msg, JsonRequestBehavior. AllowGet );
}
Server storage:
Using System;
Using System. Collections. Generic;
Using System. IO;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;
Using System. Web;

Namespace WMJQ. Common
{
Public static class UploadHelper
{
/// <Summary>
/// Upload images
/// </Summary>
/// <Param name = "CustomID"> customer ID </param>
/// <Param name = "CConactID"> customer contact ID </param>
/// <Param name = "hp"> image resources </param>
/// <Param name = "uploadPath"> Base path </param>
/// <Returns> </returns>
Public static string UploadImage (int CustomID, int CConactID, HttpPostedFileBase hp, string uploadPath)
{
// Define an error message
String msg = "";

If (hp = null)
{
Msg = "select a file .";
}
// Get the file name
String fileName = DateTime. Now. ToString ("yyyyMMddHHmmssms") + System. IO. Path. GetExtension (hp. FileName );;
// Get the file size
Long contentLength = hp. ContentLength;
// File cannot exceed 1 MB
If (contentLength> 1024*1024*2)
{
Msg = "the file size exceeds the limit .";
}
If (! CheckFileExtension (hp. FileName ))
{
Msg = "the file type cannot be uploaded .";
}
// Save the physical path of the file
String saveFilePath = "/" + CustomID + "/" + CConactID;
String saveFile = uploadPath + saveFilePath + "/" + fileName;
Directory. CreateDirectory (uploadPath + saveFilePath );
Try
{
// Save the file
Hp. SaveAs (saveFile );
Msg = saveFilePath + "/" + fileName;
}
Catch
{
Msg = "Upload Failed .";
}
Return msg;

}


/// <Summary>
/// Check whether the file suffix meets the requirements
/// </Summary>
/// <Param name = "fileName"> </param>
/// <Returns> </returns>
Private static bool checkFileExtension (string fileName)
{
// Obtain the file suffix
String fileExtension = System. IO. Path. GetExtension (fileName );
If (fileExtension! = Null)
FileExtension = fileExtension. ToLower ();
Else
Return false;

If (fileExtension! = ". Jpeg" & fileExtension! = ". Gif" & fileExtension! = ". Png" & fileExtension! = ". Jpg ")
Return false;

Return true;
}
}
}

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.