首页 技术 正文
技术 2022年11月18日
0 收藏 705 点赞 3,249 浏览 2881 个字
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;
} }
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,024
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,514
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,362
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,776
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,854