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