当前位置:网站首页>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
边栏推荐
- groutine
- Nifi fast installation and file synchronization
- 文件操作《二》(5000字总结篇)
- Detailed explanation of Milvus 2.0 quality assurance system
- Linux MySQL data timing dump
- Production environment——
- . net type transfer
- 1-3 nodejs installation list configuration and project environment
- Error in v-on handler: "typeerror: cannot read property 'resetfields' of undefined"
- ASP. NET CORE3. 1. Solution to login failure after identity registers users
猜你喜欢
Node access to Alipay open platform sandbox to achieve payment function
Deep understanding of control inversion and dependency injection
Path environment variable
Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
Installing labellmg tutorial in Windows
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
线性代数感悟之1
[registration] tf54: engineer growth map and excellent R & D organization building
【WPF绑定3】 ListView基础绑定和数据模板绑定
快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
随机推荐
MySQL restores data through binlog file
PostgreSQL列存与行存
ASP. Net core configuration options (Part 1)
El cascade and El select click elsewhere to make the drop-down box disappear
1-5 nodejs commonjs specification
Clickhouse SQL operation
Log4j output log information to file
Shortcut keys (multiline)
Deeply understand the relevant knowledge of 3D model (modeling, material mapping, UV, normal), and the difference between displacement mapping, bump mapping and normal mapping
VsCode-Go
【WPF绑定3】 ListView基础绑定和数据模板绑定
Promise (II)
快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
Multithreaded @ async thread pool
. net type transfer
Signalr can actively send data from the server to the client
Interface document yaml
Some problems encountered in recent programming 2021 / 9 / 8
Installing labellmg tutorial in Windows
Devexpress GridView add select all columns