web api 处理发送过来的文件(图片)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Http;
using fastJSON;
using System.IO;
using System.Net.Http;
using DoMain;
using System.Web.Configuration; namespace PreAlert_WebService.Controllers
{
public class Article_Pic_Controller : ApiController
{
/// <summary>
/// 增加图片服务
/// </summary>
/// <param name="jsonParames"></param>
/// <returns></returns>
[HttpPost, HttpGet]
public string Pic_Add(string jsonParames)
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "Invalid Request!"));
} HttpRuntimeSection runTime = (HttpRuntimeSection)WebConfigurationManager.GetSection("system.web/httpRuntime"); int maxRequestLength = (runTime.MaxRequestLength) * ;
int len = System.Web.HttpContext.Current.Request.ContentLength;
string retJsonStr = "";
if (string.IsNullOrEmpty(jsonParames) || jsonParames.ToLower() == "jsonparames")
{ jsonParames = System.Web.HttpContext.Current.Request.Form["jsonParames"]; } IDictionary<string, object> entityDic = (Dictionary<string, object>)JSON.Instance.Parse(jsonParames); string guid = Guid.NewGuid().ToString();//图片唯一ID
//string app_path = AppDomain.CurrentDomain.BaseDirectory;
string app_path = System.Configuration.ConfigurationManager.AppSettings["app_path"];
string[] needParames = { "PicType", "PicSizeByte" };
//IDictionary<string, object> entityDic = (Dictionary<string, object>)JSON.Instance.Parse(jsonParames);
foreach (string needParame in needParames)
{
if (!entityDic.ContainsKey(needParame))
{
retJsonStr = "{\"ret\":\"0\",\"msg\":\"缺少必须的参数:" + needParame + "}";
return retJsonStr;
}
}
//默认png图片格式...
string PicType = entityDic["PicType"].ToString().Trim() != "" ? entityDic["PicType"].ToString() : "png";
string Description = entityDic["Description"].ToString();
string PicPath = "/articlepic/" + CollectItemID.ToString();
string PicDir = app_path + PicPath;
string FullPath = PicDir + "/" + guid + "." + PicType; int PicSizeByte = ;
int.TryParse(entityDic["PicSizeByte"].ToString(), out PicSizeByte); HttpRequest request = System.Web.HttpContext.Current.Request;
HttpFileCollection fileCollection = request.Files; // 判断是否有文件
if (fileCollection.Count > )
{
// 获取图片文件
HttpPostedFile httpPostedFile = fileCollection[];
// 文件扩展名
string fileExtension = Path.GetExtension(httpPostedFile.FileName);
// 验证图片格式
if (fileExtension.Contains(".jpg") || fileExtension.Contains(".png") || fileExtension.Contains(".bmp") || fileExtension.Contains(".gif"))
{
// 如果目录不存在则要先创建
if (!Directory.Exists(PicDir))
{
Directory.CreateDirectory(PicDir);
}
httpPostedFile.SaveAs(FullPath);
using (DBEntities db = new DBEntities())
{
Pic apic = new Pic(); apic.PicPath = PicPath + "/" + guid + "." + PicType;
apic.PicType = PicType;
apic.PicSizeByte = PicSizeByte;
db.Pic.Add(apic);
db.SaveChanges();
}
retJsonStr = "{\"ret\":\"1\",\"msg\":创建成功\"}";
return retJsonStr;
}
else
{
retJsonStr = "{\"ret\":\"0\",\"msg\":请选择jpg/png/bmp/gif格式的图片\"}";
}
}
else
{
retJsonStr = "{\"ret\":\"0\",\"msg\":图片传输错误:没有发现要上传的图片;\"}";
} retJsonStr = "{\"ret\":\"1\",\"msg\":创建成功\"}";
return retJsonStr;
} }
}
web api 处理发送过来的文件(图片)的更多相关文章
- 如果调用ASP.NET Web API不能发送PUT/DELETE请求怎么办?
理想的RESTful Web API采用面向资源的架构,并使用请求的HTTP方法表示针对目标资源的操作类型.但是理想和现实是有距离的,虽然HTTP协议提供了一系列原生的HTTP方法,但是在具体的网络环 ...
- Web API删除JSON格式的文件记录
Insus.NET的系列Web Api学习文章,这篇算是计划中最后一篇了,删除JSON格式的文件记录.前一篇<Web Api其中的PUT功能演示>http://www.cnblogs.co ...
- web api 2.0 上传文件超过4M时,出现404错误
客户端代码 string path = "C:\\text.txt"; WebClient client = new WebClient(); Uri _address = new ...
- 基于spring-boot的web应用,ckeditor上传文件图片文件
说来惭愧,这个应用调试,折腾了我一整天,google了很多帖子,才算整明白,今天在这里做个记录和分享吧,也作为自己后续的参考! 第一步,ckeditor(本博文论及的ckeditor版本4.5.6)的 ...
- C# web Api ajax发送json对象到action中
直接上代码: 1.Product实体
- 用API中的raf复制文件图片等及系统找不到指定的文件的解决办法
该运行是在eclipse中进行的操作,小白的基础理解,如有不妥之处,请大佬们指正.QQ:1055802635 package raf; import java.io.IOException;impor ...
- 如果调用.net core Web API不能发送PUT/DELETE请求怎么办?
通过阅读大佬的文章 http://www.cnblogs.com/artech/p/x-http-method-override.html想到的 通过注册中间件来解决这个问题 public void ...
- Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程
Asp.Net Core Web Api图片上传及MongoDB存储实例教程(一) 图片或者文件上传相信大家在开发中应该都会用到吧,有的时候还要对图片生成缩略图.那么如何在Asp.Net Core W ...
- ASP.NET(C#) Web Api通过文件流下载文件到本地实例
下载文件到本地是很多项目开发中需要实现的一个很简单的功能.说简单,是从具体的代码实现上来说的,.NET的文件下载方式有很多种,本示例给大家介绍的是ASP.NET Web Api方式返回HttpResp ...
随机推荐
- dedecms /member/reg_new.php SQL Injection Vul
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Dedecms会员中心注入漏洞 2. 漏洞触发条件 http://127 ...
- Ninject.MVC 知识点记录
Ninject 是跟Unity 差不多的DI容器.Ninject 推荐零配置,快速使用.小中型项目,最适合. 通过nuget,安装Ninject.MVC.略.参考博客:Ninject依赖注入 ...
- .net数据库操作
刚接触到数据库时总是被数据库中的一些基本概念,比如Connection.Command.DataReader等,给整的糊里糊涂.如今,对数据库的基本操作有了一定的认识,特此做出总结,以便后续工作中查阅 ...
- Linux 中 17 个 tar 命令实用示例
Tar(Tape ARchive,磁带归档的缩写,LCTT 译注:最初设计用于将文件打包到磁带上,现在我们大都使用它来实现备份某个分区或者某些重要的目录)是类 Unix 系统中使用最广泛的命令,用于归 ...
- python requests
快速上手http://docs.python-requests.org/zh_CN/latest/user/quickstart.html Requests 是使用 Apache2 Licensed ...
- 【原】webpack学习笔记
之前在react的项目中有用过webpack,不过没有认真的去研究,这段时间又重新好好的学习一下webpack,发觉 webpack是一个很强大的东西.而且很好用,方便,接下来主要是做一下学习的笔记 ...
- 原创最简单的ORM例子
这个仅是为了培训做的一个小例子 public class DB { public static string GetClassName(Type type) { if (type == nul ...
- git config --global core.autocrlf false
git config --global core.autocrlf false warning: LF will be replaced by CRLF in .idea/vcs.xml.The f ...
- eclipse里怎么用命令行输入args
eclipse中给java应用传args参数的方法如下:1.先写好Java代码,比如文件名为IntArrqy.java:2.在工具栏或菜单上点run as下边有个Run Configuration:3 ...
- Eclipse中修改Web项目的URL访问路径
背景 访问路径,也就是指在浏览器中访问该web系统时的根路径,比如http://localhost:8080/xxxx/index.jsp 这里的xxxx,也就是request.getContext ...