客户端上传文件

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. fullpage 全屏插件

     fullpage 全屏插件 全屏滚动效果,原生js也很好实现,主要是用 mousewheel 鼠标滚轮滚动事件, 来判断上滚动还是下滚动,之后设置每次滚动的高度为屏幕的高度即可.但是,虽然效果简单, ...

  2. <自动化测试方案_9>第九章、持续集成平台搭建

    第九章.持续集成平台搭建 (一)什么是持续集成 参考文章地址:https://blog.csdn.net/qq_32261399/article/details/76651376 敏捷软件开发(英语: ...

  3. 未能加载文件或程序集“System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项

    楼主创建项目的时候选择的是5.2.4的版本,但是后来改成了5.0.0于是出现了这个错误,解决的方法倒也简单 将View文件夹下 web.config文件中 以下两处 版本改成当前版本就行了

  4. JDBC实现简单增删改查

    JDBC全称为:Java Data Base Connectivity (java数据库连接),主要用于java与数据库的链接. 整个链接过程如下图: 1.数据库驱动:Driver 加载mysql驱动 ...

  5. windows docker redis

    拉取docker docker pull hub.c.163.com/library/redis:latest 启动docker docker run -p 6379:6379 -d hub.c.16 ...

  6. 智表ZCELL产品V1.4.0开发API接口文档 与 产品功能清单

    为了方便大家使用ZCELL,应网友要求,整理编写了相关文档,现与产品一起同步发布,供大家下载使用,使用过程中如有疑问,请与我QQ联系. 智表(ZCELL)V1.4.0版本  功能清单文档下载地址: 功 ...

  7. [LeetCode] 22. 括号生成

    题目链接:https://leetcode-cn.com/problems/generate-parentheses/ 题目描述: 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能 ...

  8. vue 绑定样式的几种方式

    vue 绑定样式 对象语法 1.v-bind:class设置一个对象,动态切换class <div :class="{'active':isActive}">xxx&l ...

  9. H5活动页开发有关

    活动页开发流程 针对各种节日各种活动,临时定稿开发的活动页,往往时间安排都比较急 ---- 产品定下需求方向 UI实现设计稿 1. 草图和交互逻辑定稿=>多少个页面,每个页面表达的含义以及和用户 ...

  10. 在C++中定义常量

    在 C++ 中,有两种简单的定义常量的方式: 使用 #define 预处理器. 使用 const 关键字 使用 #define 预处理器: #define identifier value: #inc ...