当前位置:网站首页>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
边栏推荐
- Kingdee Cloud Star API calling practice
- SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
- 拷贝构造函数 浅拷贝与深拷贝
- Sub database and sub table & shardingsphere
- ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
- _ Mold_ Board_
- How vscode compares the similarities and differences between two files
- Multithreaded @ async thread pool
- Use of shell sed command
- MySQL modify master database
猜你喜欢
Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
Detailed explanation of Milvus 2.0 quality assurance system
Further study of data visualization
Path environment variable
Shell script -- shell programming specification and variables
Sub database and sub table & shardingsphere
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
Devexpress GridView add select all columns
网络安全之渗透靶场实战详解
随机推荐
Baidu Map Case - Zoom component, map scale component
[C] thoroughly understand the deep copy
Conversion between hexadecimal numbers
【题解】[SHOI2012] 随机树
[C#] 彻底搞明白深拷贝
How vscode compares the similarities and differences between two files
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
杂文 谈谈古典的《拆掉思维里的墙》
[problem solving] [show2012] random tree
Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
Promise (I)
1-1 NodeJS
Solution architect's small bag - 5 types of architecture diagrams
Promise (IV)
Lock锁
PHP efficiently reads large files and processes data
How does matlab draw the curve of known formula and how does excel draw the function curve image?
_ Mold_ Board_
Feign report 400 processing
基于51单片机红外无线通讯仿真