Files
upload-web/mulUploadImg.aspx.cs
T
2025-02-20 15:08:30 +08:00

101 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utils;
using Utils.ImageUtils;
public partial class mulUploadImg : System.Web.UI.Page
{
public static string dPath = ConfigurationManager.AppSettings["dPath"];
private void conSuc(string msg)
{
Response.Write("{\"res\":\"1\",\"msg\":\"" + msg + "\"}");
//Response.End();
}
private void conErc(string msg)
{
Response.Write("{\"res\":\"0\",\"msg\":\"" + msg + "\"}");
//Response.End();
}
public static string GenerateStringID()
{
long i = 1;
foreach (byte b in Guid.NewGuid().ToByteArray())
{
i *= ((int)b + 1);
}
return string.Format("{0:x}", i - DateTime.Now.Ticks);
}
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Files.Count < 1)
{
conErc("空文件!");
return;
}
try
{
string ut = Request.QueryString["ut"];
HttpPostedFile postFile = Request.Files[0];
if (postFile != null)
{
string errMsg = "";
if (ut != "fp")
{
if (!CheckImage(postFile, out errMsg))
{
Response.Write("{\"res\":\"0\",\"msg\":\"不是有效的图片文件!\"}");
Response.End();
return;
}
}
//using (System.Drawing.Image imgThumb = System.Drawing.Image.FromStream(postFile.InputStream))
//{
// result = ImageMaker.ToThumbnailImages(imgThumb, saveFile, 400, "", 9, 3);
//}
string sPath = Path.Combine(dPath, ut);
if (!Directory.Exists(sPath)) Directory.CreateDirectory(sPath);
string fName = GenerateStringID() + Path.GetExtension(postFile.FileName);
string saveFile = Path.Combine(sPath, fName);
//上传文件
postFile.SaveAs(saveFile);
//conSuc(fName);
Response.Write("{\"res\":\"1\",\"msg\":\"上传成功!\",\"fn\":\"" + fName + "\"}");
return;
}
}
catch (Exception ex)
{
conErc("发生错误!" + CommonHelper.FormatTextArea(ex.Message));
return;
}
conErc("无法上传");
}
public static bool CheckImage(HttpPostedFile postedFile, out string errMsg)
{
errMsg = "";
if (postedFile == null || postedFile.FileName == "")
{
errMsg = "请选择要上传的图片";
return false;
}
if (postedFile.ContentLength > 83886080)
{
errMsg = "上传的图片大小不允许超过80MB";
return false;
}
return true;
}
}