File does not flush upload files in MVC

Source: Internet
Author: User

Objective

Uploading files should be a common and necessary operation, and there are many upload controls available on the Web. Today, a problem has been encountered: input control file cannot be asynchronously uploaded without a flush. I really feel awkward. So just try to deal with it.

Upload Package class:
UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;UsingSystem.Text;UsingSystem.Threading.Tasks;UsingSystem.IO;Usingsystem.web;Namespacecommonhelper{PublicClassUploadFile:System.Web.UI.Page {PublicUploadFile () {}//Error messagePublicString Msg {GetSet; }PublicString FullName {GetSet; }//File namePublicString FileName {GetSet; }///<summary>///File Upload///by Wyl 20161019///</summary>///<param name= "filepath" >File Upload Address</param>///<param name= "Size" >File specified size</param>///<param name= "filetype" >File type</param>///<param name= "Files" >Files uploaded by file</param>///<param name= "Isrename" >Name of duplicate</param>///<returns></returns>PublicBOOL Upload_file (String filepath,int size,String[] filetype,BOOL Isrename =False) {filepath =Server.MapPath (filepath);//Create a folder that does not existif (!Directory.Exists (filepath)) directory.createdirectory (filepath);if (HttpContext.Current.Request.Files.Count = =0) {msg ="File upload failed";ReturnFalse; } msg ="Upload successful";var file = httpcontext.current.request.files[0];if (file. ContentLength = =0) {msg ="File size is 0";ReturnFalse; }if (file. ContentLength > Size *1024) {msg ="File exceeds the specified size";ReturnFalse; }var Filex = httpcontext.current.request.files[0];String Fileext =Path.getextension (Filex. FileName). ToLower ();if (filetype. Count (A = = = Fileext) <1 "return falseif (isrename) FileName = DateTime.Now.ToString (" yyyymmddhhmmss Fileext; FileName = Filex. FileName; FullName = Path.Combine (filepath, FileName); file. SaveAs (FullName); return true;}}    

The way to upload files is not an introduction to this. < Span style= "color: #000000;" >< Span style= "color: #0000ff;" > Look at the code comments should all be understood.

Page HTML
<DivClass= "Content"><FormMethod= "POST"Target= "Hidden_frame"Enctype= "Multipart/form-data"Action= "/customfrom/formdesign/fileupload"Name= "Form"><InputClass= "M Input"Name= "FileName"Type= "File"><InputClass= "Btn File-input"Value= "Submit ..."Name= "F2"Type= "Submit"><IframeId= "Hidden_frame"Name= "F2"Style= "Display:none"><html><head></ Head><body< Span style= "color: #0000ff;" >></body> </html></< Span style= "color: #800000;" >iframe></form ></div   

The above page is the HTML definition of the upload control. There are a few things to note.

1.enctype= "Multipart/form-data" must be added, the form enctype= "Multipart/form-data" means to set the MIME encoding of the form. By default, this encoding format is application/x-www-form-urlencoded, cannot be used for file upload, only use the Multipart/form-data to complete the transfer of file data, do the following. Enctype= "Multipart/form-data" is to upload binary data; The value of input in the form is passed in 2 binary ways.

The name of 2.form should be added

3. Submit button is submit, of course, if you want to write JS set to a button also become. There's nothing to say about this.

4. The style= "Display:none" in the IFRAME

The above is the entire layout and submit the upload file to the background, and jump to Ifrom, the next step is to accept the call upload file above method. Then, on the IFRAME page, you are prompted to upload the results and then close the IFRAME.

Background code:
[HttpPost]PublicActionResult FileUpload () {//Get support for uploading file formats from the configuration filestring[] FileType = configurationmanager.appsettings["FileType"]. Split (‘|‘);// upload file path string strpath = Configurationmanager.appsettings[ "strpath ""; UploadFile file=  UploadFile (); bool flag = File.upload_file (strpath, 25000return Content (< Script>window.alert ('  ' + file.msg +  ' 

Note: File path, File save path is placed in the configuration file, of course, you can also put the file size, whether renaming is placed in the configuration file.

File does not flush upload files in MVC

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.