public void ProcessRequest(HttpContext context) { try { context.Response.AppendHeader("Access-Control-Allow-Origin", "*"); // 响应类型 context.Response.AppendHeader("Access-Control-Allow-Methods", "POST"); // 响应头设置 context.Response.AppendHeader("Access-Control-Allow-Headers", "x-requested-with,content-type"); context.Response.Charset = "gb2312"; //设置字符集类型 context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); context.Response.ContentType = "application/json;charset=gb2312"; //context.Response.ContentType = "text/json"; string account = context.Request.Params["UserAccount"]; string token = context.Request.Params["Token"]; string thumbnail = context.Request.Params["thumbnail"]; YZAuthHelper.OAuth(); //YZAuthHelper.AshxAuthCheck(); //if (!YZAuthHelper.IsAuthenticated) //{ // JsonItem rv = new JsonItem(); // rv.Attributes["success"] = false; // rv.Attributes["errorMessage"] = JosonStrings.Aspx_Upload_NoAuth; // context.Response.Write(rv.ToString()); // return; //} HttpFileCollection files = context.Request.Files; if (files.Count > 0 && files[0].ContentLength > 0) { HttpPostedFile file = files[0]; string fileName = System.IO.Path.GetFileName(file.FileName); long fileSize = file.ContentLength; string fileExt = System.IO.Path.GetExtension(fileName).ToLower(); string fileId; string savePath; do { fileId = YZAttachmentHelper.GetNewFileID(); savePath = Attachment.FileIDToPath(fileId, YZAttachmentHelper.AttachmentRootPath); } while (File.Exists(savePath)); Directory.CreateDirectory(savePath.Substring(0, savePath.LastIndexOf(@"\"))); file.SaveAs(savePath); if (!String.IsNullOrEmpty(thumbnail) && !YZStringHelper.EquName(thumbnail, "n")) { this.MakeThumbnail(savePath, "S"); this.MakeThumbnail(savePath, "M"); } Attachment attachment = new Attachment(); attachment.FileID = fileId; attachment.Name = fileName; attachment.Ext = fileExt; attachment.Size = fileSize; attachment.LastUpdate = DateTime.Now; attachment.OwnerAccount = YZAuthHelper.LoginUserAccount; using (IDbConnection cn = YZDBProviderManager.CurrentProvider.OpenConnection()) { YZDBProviderManager.CurrentProvider.InsertAttachmentInfo(cn, attachment); } JsonItem rv = new JsonItem(); rv.Attributes["success"] = true; rv.Attributes["fileid"] = fileId; rv.Attributes["Name"] = fileName; rv.Attributes["Ext"] = fileExt; rv.Attributes["Size"] = fileSize; rv.Attributes["OwnerAccount"] = attachment.OwnerAccount; rv.Attributes["LastUpdate"] = YZStringHelper.DateToStringL(attachment.LastUpdate); context.Response.Write(rv.ToString()); } else { JsonItem rv = new JsonItem(); rv.Attributes["success"] = false; rv.Attributes["errorMessage"] = JosonStrings.Aspx_Invalid_File; context.Response.Write(rv.ToString()); } } catch (Exception exp) { JsonItem rv = new JsonItem(); rv.Attributes["success"] = false; rv.Attributes["errorMessage"] = exp.Message /* + exp.StackTrace*/; context.Response.Write(rv.ToString()); } }
private JObject SaveAttachment(HttpContext context) { JObject result = new JObject(); HttpFileCollection files = context.Request.Files; if (files.Count > 0 && files[0].ContentLength > 0) { HttpPostedFile file = files[0]; string fileName = System.IO.Path.GetFileName(file.FileName); long fileSize = file.ContentLength; string fileExt = System.IO.Path.GetExtension(fileName).ToLower(); //华为手机,fileExt格式 .png?112714368714 if (!String.IsNullOrEmpty(fileExt)) { int index = fileExt.IndexOf('?'); if (index != -1) { fileExt = fileExt.Substring(0, index); } } string fileId = YZAttachmentHelper.GetNewFileID(); string savePath = Attachment.FileIDToPath(fileId, YZAttachmentHelper.AttachmentRootPath); Directory.CreateDirectory(savePath.Substring(0, savePath.LastIndexOf(@"\"))); file.SaveAs(savePath); Attachment attachment = new Attachment(); attachment.Name = fileName; attachment.Ext = fileExt; attachment.Size = fileSize; attachment.FileID = fileId; if (String.IsNullOrEmpty(attachment.Name)) { attachment.Name = fileId + attachment.Ext; } attachment.LastUpdate = DateTime.Now; attachment.OwnerAccount = YZAuthHelper.LoginUserAccount; using (IDbConnection cn = QueryManager.CurrentProvider.OpenConnection()) { QueryManager.CurrentProvider.InsertAttachmentInfo(cn, attachment); } result["success"] = true; result["fileid"] = attachment.FileID; JObject attach = new JObject(); result["attachment"] = attach; attach["FileID"] = attachment.FileID; attach["Name"] = attachment.Name; attach["Ext"] = attachment.Ext; attach["Size"] = attachment.Size; attach["FileID"] = attachment.FileID; attach["LastUpdate"] = YZStringHelper.DateToStringL(attachment.LastUpdate); attach["OwnerAccount"] = attachment.OwnerAccount; } else { throw new Exception("未上传文件"); } return(result); }