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. Bmob开发指南【android端】

    作为一个开发者,数据的存储,用户的登陆,验证等操作,对程序来说是必不可少的,下面我们将从Bmob的基本信息开始讲起: [Bmob] 官网:   http://www.bmob.cn/ 帮助文档:htt ...

  2. struts2 CVE-2012-0838 S2-007 Remote Code Execution && Hotfix

    catalog . Description . Effected Scope . Exploit Analysis . Principle Of Vulnerability . Patch Fix 1 ...

  3. Io 异常: The Network Adapter could not establish the connection 解决方法

    1.IP错误: 在设置URL时错误,例如:jdbc:oracle:thin:@192.168.1.80:1521:orcl 数据库服务器是否正确:ping 服务器IP是否通畅.ping不通则将URL更 ...

  4. A.Kaw矩阵代数初步学习笔记 2. Vectors

    “矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...

  5. JAVA获取apk包的package和launchable-activity名称(一)

    背景: 每次要获取apk包的package和launchable-activity名称都需要运行doc命令,感觉好浪费感情,因为经常记不住常常的路径,但又不想把aapt设置为环境变量 我这个工具分几步 ...

  6. 第三次作业——K米评测

    第一部分 调研,评测 1.第一次上手体验 其实让我下载一个APP并且长期使用它是一件特别难的事情,不仅是因为占空间,需要注册个人信息,绑定账号,更是因为每款软件的功能虽然都很齐全,但是你并在没有真正用 ...

  7. java函数substring()

    String str; str=str.substring(int beginIndex);截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str: str=str.sub ...

  8. BZOJ1972: [Sdoi2010]猪国杀

    “此题注意样例少了个J,且牌堆可能用完牌,若牌用完则不停取最后一张”.——hzwer 然后直接模拟,认真读题,理清思路. #include<cstdio> #include<list ...

  9. DataView

    表示用于排序.筛选.搜索.编辑和导航的 DataTable 的可绑定数据的自定义视图. DataView的功能类似于数据库的视图,他是数据源DataTable的封装对象,可以对数据源进行排序.搜索.过 ...

  10. Eclipse查看hadoop源代码出现Source not found,是因为没有添加.zip

    在我们hadoop编程中,经常遇到像看看hadoop的某个类中函数的功能.但是我们会遇到一种情况就是Source not found.遇到这个问题,该如何解决.因为我们已经引入了包,为什么会找不到.如 ...