bremen_short_url/BackEnd/api/uploadPDFImage.ashx

94 lines
3.2 KiB
Plaintext

<%@ WebHandler Language="C#" Class="uploadPDFImage" %>
using System;
using System.Web;
using System.Runtime.Serialization.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Web.Services.Protocols;
using System.Security.Cryptography;
using System.Text;
using System.Web.SessionState;
using System.Data;
public class uploadPDFImage : IHttpHandler, IReadOnlySessionState {
private string _baseurl = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath.TrimEnd('/');
public void ProcessRequest (HttpContext context) {
result objRet = new result();
DataContractJsonSerializer json = new DataContractJsonSerializer(objRet.GetType());
context.Response.ContentType = "application/json;charset=utf-8";
context.Response.AddHeader("Access-Control-Allow-Origin", "*");
authPermission objAuth = new authPermission();
if (!objAuth.isLogin())
{
objRet.ret = "no";
objRet.err_code = "0001";
objRet.message = "尚未登入,請登入後使用";
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
string picFile = (context.Request["picFile"] == null) ? "" : context.Request["picFile"].ToString();
string filename = (context.Request["filename"] == null) ? "" : context.Request["filename"].ToString();
string filetype = (context.Request["filetype"] == null) ? "" : context.Request["filetype"].ToString();
string fileClientName = globalClass.CreateCaseRandomCode(16);
if (!(filetype == "image/jpeg" || filetype == "image/png" || filetype == "application/pdf" || filetype == "image/jpg")) {
objRet.ret = "no";
objRet.err_code = "0002";
objRet.message = "不支援檔案格式為" + filetype;
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
if (filetype == "application/pdf") {
fileClientName = fileClientName + ".pdf";
}
if (filetype == "image/jpeg" || filetype == "image/jpg") {
fileClientName = fileClientName + ".jpg";
}
if (filetype == "application/png") {
fileClientName = fileClientName + ".png";
}
string filePath = string.Format("upload_files/{0}", fileClientName).Replace("/", "\\");
string path = System.Web.HttpContext.Current.Server.MapPath("~/");
globalClass.Base64ToFile(picFile, path + filePath);
objRet.fileName = filename;
objRet.fileLinkName = fileClientName;
objRet.fileUrl = _baseurl + "/upload_files/" + fileClientName;
objRet.fileType = filetype;
objRet.ret = "yes";
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
public class result
{
public string ret = "no";
public string err_code = "0000";
public string message = "";
public string fileName = "";
public string fileUrl = "";
public string fileType = "";
public string fileLinkName = "";
}
public bool IsReusable {
get {
return false;
}
}
}