当前位置:网站首页>Webapi + form form upload file

Webapi + form form upload file

2022-04-23 17:10:00 Tomato Warrior

There are still some problems when uploading , List at the beginning of the article , I hope to help people with the same problems as me

Question 1 :GET Ask for something to do with POST request

       At first I used GET request , I can't find out , Later changed to POST, The specific reason is not clear , However, it is generally used to send data to the server POST Well , Later, it was found that GET How to upload files , But it doesn't seem to be much .

Question two :FROM There is no... In the form  enctype="multipart/form-data" attribute

       I checked the information on the Internet :enctype Property specifies how form data should be encoded before it is sent to the server . The explanation is shown in the following table :

value describe
application/x-www-form-urlencoded Encode all characters before sending ( Default )
multipart/form-data

No character encoding .

When using a form that contains a file upload control , You must use this value .

text/plain Space to "+" plus , But no special character encoding .

Question 3 :input The form control has not been added   name attribute   

        Not added name attribute , I don't know you backstage ? This doesn't take time to check , Put it aside for a while .

The code is pasted directly below , The first is the front-end code

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript" src="jquery1.9.1.js" ></script>
	</head>
	<body>
		<form  enctype="multipart/form-data" action="http://localhost:42031/api/home/AddTableByDT" method="post">
			<input name="file" type="file" />
			<input type="submit" />
		</form>
	</body>
</html>

There's nothing to say about the front-end code , Here is the back-end code

        [HttpPost]
        public void AddTableByDapper()
        {
            try
            {
                System.Web.HttpFileCollection file = System.Web.HttpContext.Current.Request.Files;
                if (file.Count > 0)
                {
                    // file name   
                    string name = file[0].FileName;
                    // Save the file   
                    string path = HttpContext.Current.Server.MapPath("~/UpLoad/") + name;
                    file[0].SaveAs(path);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

         For back-end code , I have something to say , In the course of debugging , We can see some properties in the file


ContentLength : Is the size of the file ( In bytes )

ContentType : file type

FileName : file name

If you plan to debug locally , So first solve the cross domain problem , There are many ways to solve cross domain problems online , I won't be tired of telling .

         Generally, after uploading files , We need to limit the type and size of files , And rename the file , With these three attributes , It's all done , Originally intended to encapsulate a method , But I'm too lazy , Let's do it first . Knock the code, your eyes are so sour , Look at the picture and have a rest , Upload again when you have a chance demo

        






版权声明
本文为[Tomato Warrior]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230553458082.html