当前位置:网站首页>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
边栏推荐
- Smart doc + Torna generate interface document
- Milvus 2.0 质量保障系统详解
- Your brain expands and shrinks over time — these charts show how
- 手写事件发布订阅框架
- ASP. NET CORE3. 1. Solution to login failure after identity registers users
- 1-3 nodejs installation list configuration and project environment
- oracle 中快速获取表的列名列表
- How to implement distributed locks with redis?
- STM32__ 03 - beginner timer
- First knowledge of go language
猜你喜欢
RPC核心概念理解
Smart doc + Torna generate interface document
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
C语言函数详解
feign报400处理
. net cross platform principle (Part I)
. net type transfer
自定义my_strcpy与库strcpy【模拟实现字符串相关函数】
[pimf] openharmony paper Club - what is the experience of wandering in ACM survey
Net standard
随机推荐
Shell - introduction, variables, and basic syntax
Aiot industrial technology panoramic structure - Digital Architecture Design (8)
ClickHouse-数据类型
Rtklib 2.4.3 source code Notes
C语言函数详解
2.Electron之HelloWorld
Go language RPC communication
_ Mold_ Board_
Calculation formula related to tolerance analysis
[registration] tf54: engineer growth map and excellent R & D organization building
Preliminary understanding of promse
Clickhouse table engine
Handwritten event publish subscribe framework
ASP. Net core dependency injection service life cycle
Read a blog, re understand closures and tidy up
Promise (III)
Detailed explanation of Milvus 2.0 quality assurance system
1-4 configuration executable script of nodejs installation
1-2 characteristics of nodejs
VsCode-Go