当前位置:网站首页>asp. Net coree file upload and download example
asp. Net coree file upload and download example
2022-07-24 18:57:00 【L-960】
asp.net core image springboot Very similar , We're from a springboot File upload and download examples to asp.net core File upload and download :
this paper asp.net core Version is :6.0
1 springboot How to upload and download files in
1.1 Upload
stay controller In the receiving MultipartFile that will do .
Front end needs to use form-data The ginseng , The way is as follows :
2.2 download
Refer to this article :
spring controller There are two ways to download files
2 Use asp.net core Upload and download files
Or that sentence , Although the official website has been very detailed , But it still looks very abstract 、 obscure .
Here are a few examples to complete the upload and download of files .
2.1 Upload of files
1、 First create a controller
stay asp.net core Of api In service , We need to inherit from ControllerBase class .
[Route("[controller]/[action]")] It means , For example, we have a way uploadFile(), Then the current parameter url Namely :ip:port/File/uploadFile
[Route("[controller]/[action]")]
[ApiController]
public class FileController : ControllerBase{
}
2、 Then we create a class with parameters
Be careful : there IFormFile For standard springboot Medium MultipartFile, Can receive files directly .
For specific properties and methods, please refer to Microsoft official website .
public class FileParam
{
// file id
public long? fileId {
get; set; }
// file
public IFormFile? file{
get; set;}
}
3、 Start writing specific front-end controllers
Be careful , We need to add [FromForm], Otherwise, we cannot receive the data from the front end .
When we add [FromForm] after ,asp.net core The received parameters will be automatically encapsulated as FileParam class
[Route("[controller]/[action]")]
[ApiController]
public class FileController : ControllerBase{
[HttpPost]
public ResultVo<bool> uploadFile([FromForm] FileParam fileParam)
{
// Upload files
string? fileName = fileParam.file?.FileName;
// Get the directory of the program
string baseDir = System.AppDomain.CurrentDomain.BaseDirectory + "/upload/";
string filePath = baseDir + fileName;
// If the directory does not exist , Create
if (!System.IO.Directory.Exists(baseDir)) System.IO.Directory.CreateDirectory(baseDir);
// Create a file stream
FileStream stream = System.IO.File.Create(filePath);
// Write the uploaded file to the stream
fileParam.file?.CopyTo(stream);
// Closed flow
stream.Close();
return ResultUtil.success(true);
}
}
4、 How to transfer parameters from the front end
Use form-data The ginseng , The way is as follows
Front end needs to use form-data The ginseng , The way is as follows :
2.2 Download files
Start writing specific front-end controllers
Be careful , Our return value must be IActionResult Or its Subclass 、 Generic
[Route("[controller]/[action]")]
[ApiController]
public class FileController : ControllerBase{
public IActionResult download()
{
string url = @"D:\xxx\xxx";
// Read file information
FileInfo fileMetaInfo = new System.IO.FileInfo(url!);
// Server file path
string strFilePath = url!;
// Create file read stream
FileStream fileStream = System.IO.File.OpenRead(strFilePath);
// Get the... Of the file according to the file suffix ContentType
var fileExtensionContentTypeProvider = new FileExtensionContentTypeProvider();
// Return file contentType type
var contentType = fileExtensionContentTypeProvider.Mappings[".png"];
return File(fileStream, contentType, fileMetaInfo.Name);
}
}
边栏推荐
- Data analysis of network security competition of national vocational college skills competition digital forensics-a
- 卷积神经网络感受野计算指南
- Ionic4 learning notes 13 - Classification List of an East Project
- 网络安全80端口—-PHP CGI参数注入执行漏洞
- Converter
- FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(下)
- 理解corners_align,两种看待像素的视角
- Nacos简介和控制台服务安装
- 理解动态计算图,requires_grad、zero_grad
- Ionic4 learning notes 4 -- add a tab page
猜你喜欢

Nftscan and port3 have reached strategic cooperation in the field of NFT data

OPENGL学习(三)GLUT二维图像绘制

Understand dynamic calculation diagram, requires_ grad、zero_ grad

Today's sleep quality record 79 points

Convolution neural network receptive field calculation Guide
![[today in history] July 24: caldera v. Microsoft; Amd announced its acquisition of ATI; Google launches chromecast](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 24: caldera v. Microsoft; Amd announced its acquisition of ATI; Google launches chromecast

Ionic4 learning notes 3

LTSpice software power settings

卷积神经网络感受野计算指南

Crazy God redis notes 11
随机推荐
Vsftpd2.3.4-端口渗透 6200 irc_3281_backdoor
JDBC batch inserts 100000 /1million pieces of data
Sequences, time series and prediction in tessorflow quizs on coursera (I)
ETL开发工具Kettle下载安装环境搭建及使用教程
OPENGL学习(四)GLUT三维图像绘制
【微信小程序开发】自定义tabBar案例(定制消息99+小红心)
Ionic4 learning notes 9 -- an east project 01
狂神redis笔记11
Ionic4 learning notes 11 - popular goods display of an East Project
Tcl/tk grouping and replacement rules
leetcode-记忆化深搜/动态规划v2
Ionic4 learning notes 10 rotation map of an East Project
Those gods on Zhihu reply
QT - animation frame
第4章 复合类型
Vsftpd2.3.4 port penetration 6200 IRC_ 3281_ backdoor
Summer Niuke multi school 1:i chiitoitsu (expectation DP, inverse yuan)
Understand dynamic calculation diagram, requires_ grad、zero_ grad
投资的新阶段
Getaverse,走向Web3的远方桥梁