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.

110 lines
4.2 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;
using TradeManage;
using NetLibrary.Log;
using FastJSON;
using Newtonsoft.Json;
namespace TradeManageNew.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;
PagesNew.SaveLog(1, "上传文件", "JC_Resource" + new JavaScriptSerializer().Serialize(md), 1);
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);
}
else
{
string filepath = FullDirectoryName + "/" + filename;
files[0].SaveAs(filepath);
}
context.Response.End();
}
else
{
context.Response.Write("0");
}
}
public bool ThumbnailCallback()
{
return false;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}