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 处理发送过来的文件(图片)的更多相关文章

  1. 如果调用ASP.NET Web API不能发送PUT/DELETE请求怎么办?

    理想的RESTful Web API采用面向资源的架构,并使用请求的HTTP方法表示针对目标资源的操作类型.但是理想和现实是有距离的,虽然HTTP协议提供了一系列原生的HTTP方法,但是在具体的网络环 ...

  2. Web API删除JSON格式的文件记录

    Insus.NET的系列Web Api学习文章,这篇算是计划中最后一篇了,删除JSON格式的文件记录.前一篇<Web Api其中的PUT功能演示>http://www.cnblogs.co ...

  3. web api 2.0 上传文件超过4M时,出现404错误

    客户端代码 string path = "C:\\text.txt"; WebClient client = new WebClient(); Uri _address = new ...

  4. 基于spring-boot的web应用,ckeditor上传文件图片文件

    说来惭愧,这个应用调试,折腾了我一整天,google了很多帖子,才算整明白,今天在这里做个记录和分享吧,也作为自己后续的参考! 第一步,ckeditor(本博文论及的ckeditor版本4.5.6)的 ...

  5. C# web Api ajax发送json对象到action中

    直接上代码: 1.Product实体

  6. 用API中的raf复制文件图片等及系统找不到指定的文件的解决办法

    该运行是在eclipse中进行的操作,小白的基础理解,如有不妥之处,请大佬们指正.QQ:1055802635 package raf; import java.io.IOException;impor ...

  7. 如果调用.net core Web API不能发送PUT/DELETE请求怎么办?

    通过阅读大佬的文章 http://www.cnblogs.com/artech/p/x-http-method-override.html想到的 通过注册中间件来解决这个问题 public void ...

  8. Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程

    Asp.Net Core Web Api图片上传及MongoDB存储实例教程(一) 图片或者文件上传相信大家在开发中应该都会用到吧,有的时候还要对图片生成缩略图.那么如何在Asp.Net Core W ...

  9. ASP.NET(C#) Web Api通过文件流下载文件到本地实例

    下载文件到本地是很多项目开发中需要实现的一个很简单的功能.说简单,是从具体的代码实现上来说的,.NET的文件下载方式有很多种,本示例给大家介绍的是ASP.NET Web Api方式返回HttpResp ...

随机推荐

  1. POJ 2785 4 Values whose Sum is 0(想法题)

    传送门 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 20334   A ...

  2. 屠蛟之路_集木成舟_ForthDay

    下数据库大山,行数里至水岸,无边无际的东海便豁然展现在屠蛟少年的眼前. 要想到达东海之中的蛟灵岛绞杀beta怪蛟,夺回心爱的小公举,少年们首先需要一艘经得起风浪的船.毕竟海上之路暗涌潜伏.同样凶险万分 ...

  3. HTTPS原理

    谣言粉碎机前些日子发布的<用公共WiFi上网会危害银行账户安全吗?>,文中介绍了在使用HTTPS进行网络加密传输的一些情况,从回复来看,争议还是有的.随着网络越来越普及,应用越来越广泛,一 ...

  4. node项目换了环境node_modules各种报错

    依赖文件里,看的出来有些是 ELF 的文件,就是说有些文件是可执行程序,不是拿 js 写的,所以说这部分程序不能适合 Linux .从路径来看,这部分程序的源代码是js写的,但实际调用的是js编译之后 ...

  5. dotnetbar入门

    1.下载dotnetbar组件 2.工具箱引用 3.项目引用 4.开始工作 //此处Form完整的名称是System.Windows.Forms.Form,表示FrmMain窗体类是继承于System ...

  6. 利用POI 技术动态替换word模板内容

    项目中需要实现一个功能,动态替换给定模板里面的内容,生成word文档提供下载功能. 中间解决了问题有: 1.页眉的文档logo图片解决,刚开始的时候,HWPFDocument 对象无法读取图片对象(已 ...

  7. MongoDB的安装及CURD操作

    MongoDB的下载地址:http://www.mongodb.org/downloads MongoDB有32bit和64bit两个版本,32bit只能存放2GB数据.我们选择64bit版进行下载. ...

  8. css006 文本格式化

    css006 文本格式化 文本格式化:字体(font-family).颜色(color).字号(font-size). 行距(line-height).粗体(font-weight).斜体(font- ...

  9. Ztree的简单使用和后台交互的写法(二)

    针对Ztree的简单使用和后台交互的写法(一)中的树进行改进 1.增加节点的权限 由页面的当前用户,决定树的根节点 然后动态获取树的详细节点: 初始化函数为: function init(){ //初 ...

  10. php ceil() 函数向上舍入为最接近的整数。

    代码: <?php echo(ceil(0.60); echo(ceil(0.40); echo(ceil(); echo(ceil(5.1); echo(ceil(-5.1); echo(ce ...