C# webapi 上传下载图片
客户端上传文件
string url = url + "webUploadFile";
Uri server = new Uri(url);
HttpClient httpClient = new HttpClient(); MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent(); StreamContent streamConent = new StreamContent(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)); multipartFormDataContent.Add(streamConent, "jpg", fileName); HttpResponseMessage responseMessage = httpClient.PostAsync(server, multipartFormDataContent).Result;
return responseMessage;
服务端接收文件
[Route("webUploadFile"), System.Web.Http.HttpPost]
public HttpResponseMessage webUploadFile()
{
if (HttpContext.Current.Request.Files.AllKeys.Any())
{var httpPostedFiles = HttpContext.Current.Request.Files;
if (httpPostedFiles != null && httpPostedFiles.Count > )
{
// 获取文件
HttpPostedFile httpPostedFile = httpPostedFiles[];
string fileExtension = ".jpg";// Path.GetExtension(httpPostedFile.FileName);// 文件扩展名
string fileId = "";
string filePath = uploadPath + fileId + fileExtension;// 上传路径 httpPostedFile.SaveAs(filePath); string jsonres = "{\"code\":\"200\",\"message\":\"文件上传成功\", \"data\":{ \"fileUrl\":\"" + downloadurl+ "webDownloadFile?fileId=" + fileId + "\"}}"; return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(jsonres, System.Text.Encoding.UTF8, "application/json")};
}
}
}
服务端下载文件
[Route("webDownloadFile"), System.Web.Http.HttpGet]
public HttpResponseMessage webDownloadFile()
{
if (HttpContext.Current.Request.Params.Count > && HttpContext.Current.Request["fileId"] != null)
{
string fileId = HttpContext.Current.Request["fileId"];
string fileName = fileId + ".jpg";
string path = uploadPath + fileName;
if (!File.Exists(path))
{
return new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(Common.ReturnMessage("", "文件不存在"), System.Text.Encoding.UTF8, "application/json") };
}
HttpResponseMessage result = null;
FileStream fs = new FileStream(path, FileMode.Open);
result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(fs);
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpeg");
return result;
}
return new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(Common.ReturnMessage("", "文件下载失败"), System.Text.Encoding.UTF8, "application/json") };
}
C# webapi 上传下载图片的更多相关文章
- WebService上传下载图片
WebService服务端 接受要上传的图片 public string UploadImg(byte[] fileBytes, int id) { try { string filePath = M ...
- nodejs, 阿里oss上传下载图片
const archiver = require('archiver')const send = require('koa-send')const oss = require('ali-oss').W ...
- webApi上传下载文件
上传文件通过webApi html端调用时包含(form提交包含 enctype="multipart/form-data",才可以启作用获取到文件) public class U ...
- spring boot上传 下载图片。
https://blog.csdn.net/a625013/article/details/52414470 build.gradle buildscript { repositories { mav ...
- JavaWeb 文件上传下载
1. 文件上传下载概述 1.1. 什么是文件上传下载 所谓文件上传下载就是将本地文件上传到服务器端,从服务器端下载文件到本地的过程.例如目前网站需要上传头像.上传下载图片或网盘等功能都是利用文件上传下 ...
- 转载:JavaWeb 文件上传下载
转自:https://www.cnblogs.com/aaron911/p/7797877.html 1. 文件上传下载概述 1.1. 什么是文件上传下载 所谓文件上传下载就是将本地文件上传到服务器端 ...
- WebApi2 文件图片上传下载
Asp.Net Framework webapi2 文件上传与下载 前端界面采用Ajax的方式执行 一.项目结构 1.App_Start配置了跨域访问,以免请求时候因跨域问题不能提交.具体的跨域配置方 ...
- 用Canvas+Javascript FileAPI 实现一个跨平台的图片剪切、滤镜处理、上传下载工具
直接上代码,其中上传功能需要自己配置允许跨域的文件服务器地址~ 或者将html文件贴到您的站点下同源上传也OK. 支持: 不同尺寸图片获取. 原图缩小放大. 原图移动. 选择框大小改变. 下载选中的区 ...
- .Net Core 图片文件上传下载
当下.Net Core项目可是如雨后春笋一般发展起来,作为.Net大军中的一员,我热忱地拥抱了.Net Core并且积极使用其进行业务的开发,我们先介绍下.Net Core项目下实现文件上传下载接口. ...
随机推荐
- 《JavaScript高级程序设计》笔记:事件(十三)
事件流 事件冒泡 IE的事件流叫做事件冒泡,即事件开始时由最具体的元素接收,然后逐级向上传播到较为不具体的节点(文档).如下代码: <body> <div id="myDi ...
- js函数式编程术语总结 - 持续更新
参考文档1 参考文档2 函数式编程术语 高阶函数 Higher-Order Functions 以函数为参数的函数 返回一个函数的函数 函数的元 Arity 比如,一个带有两个参数的函数被称为二元函数 ...
- Linux下使用yum安装软件命令
1.yum list | grep 要下载的文件名字2.yum install 完整文件名字3.rpm -qa | grep 软件名字 //查看版本
- C# 离线人脸识别 ArcSoft V2.0 Demo
本来打算做个C#版demo,但没用成功.使用虹软最新人脸识别技术开发完成 过程如下: 1. 传入一张单人脸照片: 2.调用检测人脸函数ASFDetectFaces,成功返回人脸信息的指针: 3.使用 ...
- 解决Angular2 (SystemJS) XHR error (404 Not Found) loading traceur
初学Angular2,跟着Angular2中文网学到HTTP这一节时出现了一个异常: GET http://localhost:3000/traceur 404 (Not Found) Error: ...
- Delphi 字符串转日期,强大到窒息,VarToDateTime 解决了 困扰很久的小问题
procedure THRForm.Button1Click(Sender: TObject); var D:TDateTime; s:string; begin D:=VarToDateTime(' ...
- java连接sql server数据库(使用用户sa)
一.安装数据库相关软件 sql server management studio是管理sql server数据库的软件,想要使用需到微软官网下载安装sql server,然后再安装sql server ...
- CRC32明文攻击
明文攻击是一种较为高效的攻击手段,大致原理是当你不知道一个zip的密码,但是你有zip中的一个已知文件(文件大小要大于12Byte)时, 因为同一个zip压缩包里的所有文件都是使用同一个加密密钥来加密 ...
- Excel自定义公式,类似VLOOKUP的查询
Excel在使用VLOOKUP时,当检索值超过255长度的时候就会报错,没法正常检索. 官方提供的办法是通过INDEX和MATCH公式组合使用来解决. 微软官方方案 1,公式 =INDEX($A$5: ...
- ABP之启动配置
ASP.NET Boilerplate提供了在StartUp中配置其模块的基础设施和模型. 配置ASP.NET Boilerplate 配置ABP是在模块的PreInitialize 方法中做的,如下 ...