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. Java JVM、JNI、Native Function Interface、Create New Process Native Function API Analysis

    目录 . JAVA JVM . Java JNI: Java Native Interface . Java Create New Process Native Function API Analys ...

  2. 使用 BASH 作为 CGI 进行 HTTP 文件上传

    憋半天憋出这么点代码来,暂时凑合可以用...需要手动删除文件末尾的分隔符,还有一个windows 换行: #!/bin/bash newline="" while true; do ...

  3. JZOJ P1817:[8.27]研究性学习作业

    传送门 老师良心推荐的二分题.7月29号第一次写,想到了裸的DP,乱搞搞过了6组,欲优化,无解,弃疗. 然后今天老师给了题解,简单看了一下. 正解是二分答案+DP验证. 二分天数$day$,然后对于每 ...

  4. SSH(Struts Spring Hibernate开发框架)

    Spring(Model) Spring的核心思想是IoC和AOP,Spring使得管理对象更加方便,极大的降低组件之间的耦合度,实现了软件各层之间的解耦. Struts(View) 使用Struts ...

  5. C#中值类型和引用类型图解

    举几个值类型和引用类型的内存配置: 值类型存储在栈中,引用类型堆里: 1,数组 数组是引用类型,但是数组的元素可以是值类型或引用类型 2. 结构 结构是值类型,简略的看个例子 struct sampl ...

  6. android语音识别和合成第三方 .

    讯飞语音云 http://open.voicecloud.cn/index.php 目前支持6大类型的SDK下载,包括Android. iPhone平台移动应用的接入,Windows.Linux平台P ...

  7. sudo 出现unable to resolve host 解决方法

    inux 环境, 假设这台机器名字叫dev(机器的hostname), 每次执行sudo 就出现这个警告讯息:sudo: unable to resolve host dev虽然sudo 还是可以正常 ...

  8. Photoshop 融合属性 Unity Shader

    http://forum.unity3d.com/threads/free-photoshop-blends.121661/

  9. Sencha Touch 2.2 Store Proxy 异常监控

    移动端到服务端通信往往会发生很多莫名的异常情况,如何有效的监控proxy异常,给用户友好的用户体验呢? Proxy给我提供了异常exception的监听事件,只需要监控该项目即可. Sencha To ...

  10. animate实例介绍

    1.animate,setInterval组合不断滚动: function Tscroll(){ $().animate(); } setInter(); //注意里面的是 Tscroll(),而不是 ...