.netcore 文件上传转为base64位字符串
.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位字符串的更多相关文章
- 文件上传 和 base64编码
base64编码 1.关于Base64编码 : https://www.cnblogs.com/liyiwen/p/3814968.html (个人猜测),file表单发送文件,肯定是将文件转换为 ...
- NetCore文件上传校验返回未授权401,文件仍然执行上传操作,要如何解决呢
这是代码:https://files.cnblogs.com/files/suterfo/NetCoreTestPro.rar 运行步骤: 一.使用Visual Studio2017打开项目,并F5运 ...
- js 图片压缩上传(base64位)以及上传类型分类
一.input file上传类型 1.指明只需要图片 <input type="file" accept='image/*'> 2.指明需要多张图片 <input ...
- NetCore3.0 文件上传与大文件上传的限制
NetCore文件上传两种方式 NetCore官方给出的两种文件上传方式分别为“缓冲”.“流式”.我简单的说说两种的区别, 1.缓冲:通过模型绑定先把整个文件保存到内存,然后我们通过IFormFile ...
- vue + element 文件上传 并将文件转 base64
当时有一个需求 是需要用到上传文件这个功能,并且需要将文件转为 base64 给到后台.网上找的全是啥图片转base64 肯定是因为这类需求比较常见.当时有点懵了.后面一想,都他娘是文件啊.然后就找到 ...
- 文件上传三:base64编码上传
介绍三种上传方式: 文件上传一:伪刷新上传 文件上传二:FormData上传 文件上传三:base64编码上传 Flash的方式也玩过,现在不推荐用了. 优点: 1.浏览器可以马上展示图像,不需要先上 ...
- Base64文件上传(Use C#)
Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,它是一种基于64个可打印字符来表示二进制数据的方法. 使用base64进行文件上传的具体流程是:前台使用js将文件转换为base64格 ...
- .NetCore WebApi利用Swagger文档实现选择文件上传
介绍 实现这个功能主要还是依赖过滤器 在Swagger中利用 IOperationFilter 操作来实现文件上传 与之前处理结合Idr4授权一样的处理方式,不同的是授权处理的是Security,而文 ...
- chunkupload 文件上传断点续传组件(java) - 正式发布
chunkupload简介 chunkupload是一款基于java语言的断点续传组件,针对文件上传,非文件下载,集成方便,使用简单. chunkupload实现如下功能: · 实现断点续传 · ...
随机推荐
- Dynamics 365 Portal 修改注册页面及Profile页面
一,Profile页面 客户要求在Portal Profile页面上添加性别字段,通过查看源代码发现,中间的联系人信息部分是引用的CRM中Contact实体的Portal Web Form表单,直接把 ...
- OWASP ModSecurity Core Rule Set (CRS)的基本使用
Preface 前述文章开源WAF工具ModSecurity,介绍了ModSecurity作为Nginx的动态加载模块的基本安装和使用. 本篇简单介绍ModSecurity CRS规则集的使用. # ...
- Azure 上通过Automation 实现定时开关虚拟机
更多内容,请关注公众号: Azure Automation 可以提供一些自动化的功能,比如我们可以指定在每天早上6点开启虚拟机,每天晚上8点关闭虚拟机.同时还提供一些基于监控参数的自动化配置.今天的主 ...
- Maven pom.xml文件深度学习
本文介绍Maven项目构建中,pom.xml文件的生成规则和常用节点的使用方法.pom.xml官方网址:http://maven.apache.org/pom.html pom简介 pom作为项目对象 ...
- 微信小程序-强制手机端更新
小程序的更新机制 开发者在管理后台发布新版本的小程序之后,如果某个用户本地有小程序的历史版本,此时打开的可能还是旧版本.微信客户端会有若干个时机去检查本地缓存的小程序有没有更新版本,如果有则会静默更新 ...
- latex 添加Bibtex 全解(使用TeXstudio)
前提知识: 生成pdf的一般流程 在使用Latex之前,我们一般会借用已有的论文模板,在模板基础上加入我们自己的文章内容,随后编译成PDF文件,其基本流程就是:Latex->Bibtex-> ...
- BlockStack常见词语
Browser: 用户用来浏览并使用基于 blockstack 网络开发的 app. CLI: Cli 工具用来管理个人的 blockstack id. blockstack.js (and othe ...
- 初始 Tronado
安装 pip 安装 pip install tronado 手动安装 下载tronado安装包(https://pypi.python.org/packages/source/t/tornado/to ...
- 字符串 string方法
字符串 name = 'ab c dd' i = name.find('a', 1, 3) # 找到返回对应下标 找不到返回-1 print(i) j = name.rfind('d') # 寻找对应 ...
- tensorflow 性能调优相关
如何进行优化tensorflow 将极大得加速机器学习模型的训练的时间,下面是一下tensorflow性能调优相关的阅读链接: tensorflow 性能调优:http://d0evi1.com/te ...