.netcore文件上传Api接口,和正常的webForm提交类似,只是用postman测试接口时,记得给form表单命名,否则获取上传文件数量一直为0

  后端代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyApiCommon; namespace MyApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class FileController : ControllerBase
{ [HttpPost]
[Route("postfile")]
public string UploadAsync()
{
try
{
var files = HttpContext.Request.Form.Files;
if (files.Count < )
return "请上传文件";
long fileSize = files.Sum(f => f.Length) / ;//由字节转为kb
Stream fs = files[].OpenReadStream();//将文件转为流
return Base64Convert.FileToBase64(fs); }
catch (Exception ex)
{
return ex.Message;
} }
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Text; namespace MyApiCommon
{
public class Base64Convert
{
/// <summary>
/// 文件转换成Base64字符串
/// </summary>
/// <param name="fs">文件流</param>
/// <returns></returns>
public static String FileToBase64(Stream fs)
{
string strRet = null; try
{
if (fs == null) return null;
byte[] bt = new byte[fs.Length];
fs.Read(bt, , bt.Length);
strRet = Convert.ToBase64String(bt);
fs.Close();
}
catch (Exception ex)
{
throw ex;
} return strRet;
} /// <summary>
/// Base64字符串转换成文件
/// </summary>
/// <param name="strInput">base64字符串</param>
/// <param name="fileName">保存文件的绝对路径</param>
/// <returns></returns>
public static bool Base64ToFileAndSave(string strInput, string fileName)
{
bool bTrue = false; try
{
byte[] buffer = Convert.FromBase64String(strInput);
FileStream fs = new FileStream(fileName, FileMode.CreateNew);
fs.Write(buffer, , buffer.Length);
fs.Close();
bTrue = true;
}
catch (Exception ex)
{
throw ex;
} return bTrue;
}
}
}

  使用postman测试上传

.netcore 文件上传转为base64位字符串的更多相关文章

  1. 文件上传 和 base64编码

    base64编码 1.关于Base64编码  :  https://www.cnblogs.com/liyiwen/p/3814968.html (个人猜测),file表单发送文件,肯定是将文件转换为 ...

  2. NetCore文件上传校验返回未授权401,文件仍然执行上传操作,要如何解决呢

    这是代码:https://files.cnblogs.com/files/suterfo/NetCoreTestPro.rar 运行步骤: 一.使用Visual Studio2017打开项目,并F5运 ...

  3. js 图片压缩上传(base64位)以及上传类型分类

    一.input file上传类型 1.指明只需要图片 <input type="file" accept='image/*'> 2.指明需要多张图片 <input ...

  4. NetCore3.0 文件上传与大文件上传的限制

    NetCore文件上传两种方式 NetCore官方给出的两种文件上传方式分别为“缓冲”.“流式”.我简单的说说两种的区别, 1.缓冲:通过模型绑定先把整个文件保存到内存,然后我们通过IFormFile ...

  5. vue + element 文件上传 并将文件转 base64

    当时有一个需求 是需要用到上传文件这个功能,并且需要将文件转为 base64 给到后台.网上找的全是啥图片转base64 肯定是因为这类需求比较常见.当时有点懵了.后面一想,都他娘是文件啊.然后就找到 ...

  6. 文件上传三:base64编码上传

    介绍三种上传方式: 文件上传一:伪刷新上传 文件上传二:FormData上传 文件上传三:base64编码上传 Flash的方式也玩过,现在不推荐用了. 优点: 1.浏览器可以马上展示图像,不需要先上 ...

  7. Base64文件上传(Use C#)

    Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,它是一种基于64个可打印字符来表示二进制数据的方法. 使用base64进行文件上传的具体流程是:前台使用js将文件转换为base64格 ...

  8. .NetCore WebApi利用Swagger文档实现选择文件上传

    介绍 实现这个功能主要还是依赖过滤器 在Swagger中利用 IOperationFilter 操作来实现文件上传 与之前处理结合Idr4授权一样的处理方式,不同的是授权处理的是Security,而文 ...

  9. chunkupload 文件上传断点续传组件(java) - 正式发布

    chunkupload简介 chunkupload是一款基于java语言的断点续传组件,针对文件上传,非文件下载,集成方便,使用简单. chunkupload实现如下功能: ·  实现断点续传 ·  ...

随机推荐

  1. ASP.NET4.0中JavaScript脚本调用Web Service 方法

    环境:VS2019  .net 4.0 framework 根据教材使用ScriptManager在JavaScript中调用Web service 时,失败.现将过程和解决方法记录如下: 1.定义W ...

  2. php使用微信登录

    1.第一步 $hosturl = urlencode('');//异步回调地址 $wechatInfo = WechatInfo::get_wechat(); //查询appid $url = &qu ...

  3. Nginx实现负载均衡时常用的分配服务器策略

    场景 Nginx配置实例-负载均衡实例:平均访问多台服务器: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103019576 在 ...

  4. Visual Studio中相对路径中的宏定义

    $(RemoteMachine) 设置为“调试”属性页上“远程计算机”属性的值.有关更多信息,请参见更改用于 C/C++ 调试配置的项目设置. $(References) 以分号分隔的引用列表被添加到 ...

  5. 2018 经典的CVPR 关于ImageCaptioning论文

    1.        SemStyle: Learning to Generate Stylised Image Captions using Unaligned Text(2018 CVPR) 主要研 ...

  6. .net上传文件,大文件及下载方式汇总(转)

    原文地址:http://www.360doc.com/content/19/1219/10/67993814_880731215.shtml Brettle.Web.NeatUpload.dll 文件 ...

  7. Spring Cloud @RefreshScope刷新问题

    问题 使用@RefreshScope会刷新在sprign ioc中所有bean中使用@Value的值,但是在配置类中使用方法去配置的其他类参数并不会改变例如 解决方案 //使用此方法监听事件 @Eve ...

  8. JAVA集合框架(三)-Map

    前言 Map是java中用于存储键值对映射的接口.是解决编程问题最常用的数据结构之一.在工作中,有时候为实现一个功能可能写了好大一段代码,运行是ok了,但是就是不想回头再看,不敢相信自己写的这么烂.这 ...

  9. 精通awk系列(3):铺垫知识:读取文件的几种方式

    回到: Linux系列文章 Shell系列文章 Awk系列文章 读取文件的几种方式 读取文件有如下几种常见的方式: 下面使用Shell的read命令来演示前4种读取文件的方式(第五种按字节数读取的方式 ...

  10. How to: Apply Attributes to Entity Properties when Using Model First 如何:在ModelFirst时将属性应用于实体属性

    In a Model First data model, object properties are declared in the designer-generated files, and you ...