You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
4.6 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Script.Serialization;
using System.Drawing;
namespace TradeManage.GlobalAshx
{
/// <summary>
/// AjaxFileUpload 的摘要说明
/// </summary>
public class AjaxFileUpload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpFileCollection files = context.Request.Files;
string DirectoryName = context.Request.QueryString["DirectoryName"];
string width = context.Server.UrlDecode(context.Request.QueryString["width"]);
string height = context.Server.UrlDecode(context.Request.QueryString["height"]);
if (string.IsNullOrEmpty(DirectoryName) == true) DirectoryName = "ServerCookies";
string FullDirectoryName = System.AppDomain.CurrentDomain.BaseDirectory + DirectoryName;
if (Directory.Exists(FullDirectoryName) == false) Directory.CreateDirectory(FullDirectoryName);
if (files.Count > 0)
{
string ExtensionName = Path.GetExtension(files[0].FileName).ToLower();
string filename = Guid.NewGuid().ToString() + ExtensionName;
TradeData.BaseService obj = new TradeData.BaseService();
TradeModel.JC_Resource md = new TradeModel.JC_Resource();
md.FileUrl = DirectoryName + "/" + filename;
string FName = files[0].FileName;
if (FName.Contains("\\"))
FName = FName.Substring(FName.LastIndexOf('\\') + 1);
else
if (FName.Contains("/"))
FName = FName.Substring(FName.LastIndexOf('/') + 1);
md.FileName = FName;
md.FileKeyWord = FName;
md.ExtType = ExtensionName;
md.CreateTime = DateTime.Now;
long fize = files[0].InputStream.Length;
md.FileSize = fize.ToString();
md.InUserId = 0;
md.CompanyId = 0;
int id =0;
if (fize < 500000)
{
id = obj.SaveResource(md);
}
var model = new { Id = id, FileName = FName, FileUrl = md.FileUrl, FileSize = files[0].InputStream.Length };
string jsonString = new JavaScriptSerializer().Serialize(model);
context.Response.Write(jsonString);
if (width != null && height != null)
{
string filepath = System.AppDomain.CurrentDomain.BaseDirectory + "ServerCookies/" + filename;
files[0].SaveAs(filepath);
Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
Bitmap myBitmap = new Bitmap(filepath);
int thumbWidth = Convert.ToInt32(width);
int thumbHeight = Convert.ToInt32(height);
Image myThumbnail = myBitmap.GetThumbnailImage(thumbWidth, thumbHeight, myCallback, IntPtr.Zero);
Bitmap bmp1 = new Bitmap(myThumbnail);
string filepath2 = FullDirectoryName + "/" + filename;
bmp1.Save(filepath2, System.Drawing.Imaging.ImageFormat.Png);
bmp1.Dispose();
myBitmap.Dispose();
File.Delete(filepath);
////20240716新增 如果文件保存的信息不存在返回0告知
if (!File.Exists(filepath2))
{
context.Response.Clear();
context.Response.Write("0");
}
////20240716end
}
else
{
string filepath = FullDirectoryName + "/" + filename;
files[0].SaveAs(filepath);
////20240716新增 如果文件保存的信息不存在返回0告知
if (!File.Exists(filepath))
{
context.Response.Clear();
context.Response.Write("0");
}
////20240716end
}
context.Response.End();
}
else
{
context.Response.Write("0");
}
}
public bool ThumbnailCallback()
{
return false;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}