客户端上传文件

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 上传下载图片的更多相关文章

  1. WebService上传下载图片

    WebService服务端 接受要上传的图片 public string UploadImg(byte[] fileBytes, int id) { try { string filePath = M ...

  2. nodejs, 阿里oss上传下载图片

    const archiver = require('archiver')const send = require('koa-send')const oss = require('ali-oss').W ...

  3. webApi上传下载文件

    上传文件通过webApi html端调用时包含(form提交包含 enctype="multipart/form-data",才可以启作用获取到文件) public class U ...

  4. spring boot上传 下载图片。

    https://blog.csdn.net/a625013/article/details/52414470 build.gradle buildscript { repositories { mav ...

  5. JavaWeb 文件上传下载

    1. 文件上传下载概述 1.1. 什么是文件上传下载 所谓文件上传下载就是将本地文件上传到服务器端,从服务器端下载文件到本地的过程.例如目前网站需要上传头像.上传下载图片或网盘等功能都是利用文件上传下 ...

  6. 转载:JavaWeb 文件上传下载

    转自:https://www.cnblogs.com/aaron911/p/7797877.html 1. 文件上传下载概述 1.1. 什么是文件上传下载 所谓文件上传下载就是将本地文件上传到服务器端 ...

  7. WebApi2 文件图片上传下载

    Asp.Net Framework webapi2 文件上传与下载 前端界面采用Ajax的方式执行 一.项目结构 1.App_Start配置了跨域访问,以免请求时候因跨域问题不能提交.具体的跨域配置方 ...

  8. 用Canvas+Javascript FileAPI 实现一个跨平台的图片剪切、滤镜处理、上传下载工具

    直接上代码,其中上传功能需要自己配置允许跨域的文件服务器地址~ 或者将html文件贴到您的站点下同源上传也OK. 支持: 不同尺寸图片获取. 原图缩小放大. 原图移动. 选择框大小改变. 下载选中的区 ...

  9. .Net Core 图片文件上传下载

    当下.Net Core项目可是如雨后春笋一般发展起来,作为.Net大军中的一员,我热忱地拥抱了.Net Core并且积极使用其进行业务的开发,我们先介绍下.Net Core项目下实现文件上传下载接口. ...

随机推荐

  1. delphi中WMI的使用(网卡是否接入)

    WMI(Windows Management Instrumentation,Windows 管理规范)是一项核心的 Windows 管理技术:用户可以使用 WMI 管理本地和远程计算机. 通过使用W ...

  2. modbus串口通讯C#

    简介 公司给的一个小任务,这篇文章进行详细讲解 题目: modbus串口通讯 主要内容如下: 1.实现使用modbus通讯规约的测试软件: 2.具有通信超时功能: 3.分主站从站,并能编辑报文.生成报 ...

  3. 当锚点遇到fixed

    问题:页面内容导航fixed+锚点错位 solution: 锚点定位跟外边距没有关系. 我们使用内边距padding-top=fixed导航高度来使锚点可以精准定位: 此时新的问题产生了,内边距的存在 ...

  4. Ubuntu 16.04 安装GIMP绘图软件

    Ubuntu上比较好用的绘图软件,GIMP,安装方法如下: 终端输入 : sudo apt-get install gimp ,回车,输入密码,即可安装简单易行. 输入 :gimp ,启动程序.

  5. eclipse中使用Lombok(转)

    原文链接:https://www.cnblogs.com/justuntil/p/7120534.html windows环境 1.下载lombok.jar包https://projectlombok ...

  6. 数据库【mongodb篇】基本命令学习笔记

    MongoDB基本命令用 MongoDB基本命令用   成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs ...

  7. python接口自动化-post请求4

    云盘登录实操案例: 代码参考: # coding:utf-8 import requests ''' https的请求相对于http安全级别高,需要验证SSL证书 import urllib3 使用这 ...

  8. vscode 安装插件SVN 报vscode SVN not found

    1.软件环境 svn客户端安装的是TortoiseSVN: vscode 安装的为SVN的插件: 2. 问题现象 vscode打开文件夹后右下角提示如下报错:SVN not found. Instal ...

  9. Webstorm的一些常用快捷键

    ctrl+/ 单行注释ctrl+shift+/块注释Ctrl+X 删除行Ctrl+D 复制行Ctrl+B 快速打开光标处的类或方法Ctrl+F 查找文本Ctrl+R 替换文本ctrl+shift+ + ...

  10. mysql8.0 Server 在Windows平台中的安装、初始化和远程访问设置

    mysql8.0 server安装 1.下载mysql 8.0 可以到mysql官网下载 https://dev.mysql.com/downloads/mysql 或者如下地址 mysql-8.0. ...