table of Contents
2. HTML5 FormData method introduction and file upload
1 Introduction
In the next project of the MVC framework, the web end uses the form form to submit information to the background. There is a file in the submitted information, and I do n’t know how to deal with it at first. Later, I saw on the blog that other people solved it. Issues are sorted out and extended here.
2. HTML5 FormData method introduction and file upload
step:
1. Problem description
The front end submits the image through the form form, the image type is file, how to receive it in the background?
2. Examples
1. Introduction to FormData
FormData is an API provided by ES, which can be used to implement ajax file submission. fomdata is actually an object of XMLHttpRequest, use it to submit forms, simulate form submission, and use formData to transfer, the advantage is that you can transfer files and pictures.
2. Basic usage:
The FormData object can form the name and value of all form elements into a queryString and submit it to the background. Just pass the form form as a parameter to the FormData constructor:
var form = document.getElementById("form1");
var formData = new FormData (form);
3. Code writing
The htlm and js codes are directly referenced: https://blog.csdn.net/weixin_42193004/article/details/86509283#%E6%8E%A7%E5%88%B6%E5%99%A8
Console receives data
In the console receiving data, the picture of the file type is received as an instance of HttpPostedFileBase, so the HttpPostFileBase class is specifically studied below
Note: First of all, please note that because of the file upload, there are certain requirements for the size of the upload. The get request is generally up to 2k. Post theoretically there is no size limit.
3.HttpPostedFileBase 类
Namespace: System.Web
Assembly: System.Web.dll
definition
Used as a base class for a class that provides access to a single file that has been uploaded by the client.
Therefore, the files uploaded by the foreground have formal parameters of this type to be received.
ContentLength | When overridden in a derived class, get the size (in bytes) of the uploaded file. |
ContentType | When overridden in a derived class, get the MIME content type of the uploaded file. |
FileName | When overridden in a derived class, get the fully qualified name (absolute path) of the file on the client . |
InputStream | When overridden in a derived class, get a Stream object pointing to the uploaded file in preparation for reading the content of the file. |
Equals(Object) | Determines whether the specified object is equal to the current object. (Inherited from Object ) |
GetHashCode() | Used as the default hash function. (Inherited from Object ) |
GetType() | Get the Type of the current instance . (Inherited from Object ) |
MemberwiseClone() | Create a shallow copy of the current Object . (Inherited from Object ) |
SaveAs(String) | When overridden in a derived class, save the contents of the uploaded file. |
ToString() | Returns a string representing the current object. (Inherited from Object ) |