首页 » 软件优化 » NET+EF+MVC开发App接口实现视频上传(上传失败上传文件娱乐接口)

NET+EF+MVC开发App接口实现视频上传(上传失败上传文件娱乐接口)

南宫静远 2024-11-24 05:15:35 0

扫一扫用手机浏览

文章目录 [+]

public void ProcessRequest(HttpContext context)

{

context.Response.ContentType = "text/plain";

NET+EF+MVC开发App接口实现视频上传(上传失败上传文件娱乐接口) 软件优化
(图片来自网络侵删)

string ImgType = "1";

string ImgDir = context.Request["ImgDir"]; //上传文件的目标

if (string.IsNullOrEmpty(ImgDir))

{

ImgDir = "images";

}

else

{

ImgDir = ImgDir.ToLower();

}

try

{

//string ImgType = context.Request["ImgType"]; //0:H5 1:安卓

var ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"上传失败\",\"data\":[]}";

string file = "";

string strfile = "";

if (HttpContext.Current.Request.Files == null)

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"未上传文件\",\"data\":[]}";

context.Response.Write(ReturnStr);

context.Response.End();

}

int count = HttpContext.Current.Request.Files.Count;

if (count <= 0)

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"未上传文件\",\"data\":[]}";

context.Response.Write(ReturnStr);

context.Response.End();

}

// var dd = HttpContext.Current.Request.Files["uploadkey0"].ContentLength;

// context.Response.Write(Userid);

// context.Response.End();

StringBuilder sp = new StringBuilder();

if (ImgType == "1" && count > 0)

{

sp.Append("{\"result\":\"" + (int)KeyEnum.AppReturn.成功 + "\",\"msg\":\"上传成功\",\"data\":[");

}

if (count > 0)

{

for (int k = 0; k < count; k++)

{

int l = HttpContext.Current.Request.Files["uploadkey" + (k)].ContentLength;

if (l <= 0)

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"未上传文件\",\"data\":[]}";

context.Response.Write(ReturnStr);

context.Response.End();

}

byte[] buffer = new byte[l];

Stream s = HttpContext.Current.Request.Files["uploadkey" + (k)].InputStream;

System.Drawing.Bitmap image = new System.Drawing.Bitmap(s);

string imgname = Utils.Get16Num().ToString() + ".jpg";

// string path = "Images/" + DateTime.Now.ToString("yyyyMMdd") + "/";

//string imageDir = "/Images/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";

string imageDir = "/gpt001/" + "/" + ImgDir + "/";

string newFileName = Guid.NewGuid().ToString();//使用GUID作为文件的新名称,当然也可以使用其他的方式对文件进行重命名。

string fullDir = imageDir + newFileName + ".jpg";//完整的图片存储的路径

//Path.GetDirectoryName(context.Request.MapPath(imageDir));

if (!Directory.Exists(context.Request.MapPath(imageDir)))

{

System.IO.Directory.CreateDirectory(context.Request.MapPath(imageDir));

}

image.Save(HttpContext.Current.Server.MapPath(fullDir));

string phonames = null;

if (ImgDir == "babygrouth")

{

//缩略图

phonames = imageDir + "s_" + newFileName + ".jpg";

WisdomStarts.Common.Thumbnail.GetThumbnail(HttpContext.Current.Server.MapPath(imageDir + newFileName + ".jpg"), HttpContext.Current.Server.MapPath(phonames), 202, 202, 7);

}

if (ImgType == "1")

{

if (k > 0)

{

sp.Append(",");

}

sp.Append("{\"imagepath\":\"" + fullDir + "\"}");

}

else

{

if (k == count - 1)

{

strfile = strfile + fullDir;

}

else

{

strfile = strfile + fullDir + ";";

}

}

}

}

if (ImgType == "1")

{

sp.Append("]}");

context.Response.Write(sp.ToString());

}

else

{

context.Response.Write(strfile);

}

}

catch (Exception ex)

{

WisdomStarts.Common.Log.NetLog.WriteTextLog("ChatPostFile上传错误", ex.ToString());

WisdomStarts.Common.Log.NetLog.WriteTextLog("ImgDir", ImgDir);

}

}

标签:

相关文章