private JObject GetAttachments(string FileID) { JObject rv = new JObject(); if (!String.IsNullOrEmpty(FileID)) { string[] ids = FileID.Split(',', ';'); AttachmentCollection attachments; using (IDbConnection cn = QueryManager.CurrentProvider.OpenConnection()) { attachments = YZAttachmentHelper.GetAttachmentsInfo(cn, ids); } JArray ja = new JArray(); rv["files"] = ja; foreach (Attachment item in attachments) { JObject jo = new JObject(); ja.Add(jo); jo["name"] = item.Name; string requestUrl = HttpContext.Current.Request.Url.ToString(); string RedirectURLBase = requestUrl.Substring(0, requestUrl.IndexOf("YZSoft", StringComparison.OrdinalIgnoreCase)) + "YZSoft.Services.REST/Attachment/download.ashx?" + item.FileID; jo["DownloadUrl"] = RedirectURLBase; } } return(rv); }
public static Attachment GetAttachmentInfo(IDbConnection cn, string fileId) { if (String.IsNullOrEmpty(fileId)) { throw new Exception(Resources.YZStrings.Aspx_Upload_EmptyFileID); } AttachmentCollection attachments = YZAttachmentHelper.GetAttachmentsInfo(cn, new string[] { fileId }); if (attachments.Count == 0) { throw new Exception(String.Format(Resources.YZStrings.Aspx_Upload_FileIDNotFount, fileId)); } return(attachments[0]); }
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); }
public void ProcessRequest(HttpContext context) { //if (context.Request.Headers["If-None-Match"] != null || context.Request.Headers["If-Modified-Since"] != null) //{ // context.Response.Status = "304 Not Modified"; // context.Response.Cache.AppendCacheExtension("max-age=" + 365 * 24 * 60 * 60); // context.Response.Cache.SetExpires(DateTime.Now.AddYears(1)); // context.Response.AppendHeader("ETag", "Never_Modify"); // context.Response.Cache.SetETag("Never_Modify"); // context.Response.Cache.SetLastModified(DateTime.Now.AddMinutes(-1)); // context.Response.End(); // return; //} string fileId = context.Request.Params["fileid"]; try { Attachment attachment; using (IDbConnection cn = QueryManager.CurrentProvider.OpenConnection()) { attachment = YZAttachmentHelper.GetAttachmentInfo(cn, fileId); } string fileName = attachment.Name; string fileExt = attachment.Ext; long fileSize = attachment.Size; string filePath = Attachment.FileIDToPath(fileId, YZAttachmentHelper.AttachmentRootPath); if (!File.Exists(filePath)) { throw new Exception(String.Format(Net.MobileHelper.YZSoft.Resources.JosonStrings.Aspx_Upload_FileIDNotFount, fileId)); } bool contentDisposition = true; string range = context.Request.Headers["HTTP_RANGE"]; string content_type = "application/octet-stream"; RegistryKey file_key = Registry.ClassesRoot.OpenSubKey(fileExt); if (file_key != null) { content_type = file_key.GetValue("Content Type", content_type).ToString(); } context.Response.AppendHeader("Content-Type", content_type); if (contentDisposition) { context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + context.Server.UrlPathEncode(fileName)); } context.Response.AppendHeader("Accept-Ranges", "bytes"); if (range == null) { //全新下载 context.Response.AppendHeader("Content-Length", fileSize.ToString()); context.Response.CacheControl = HttpCacheability.Public.ToString(); context.Response.Cache.AppendCacheExtension("max-age=" + 365 * 24 * 60 * 60); context.Response.Cache.SetExpires(DateTime.Now.AddYears(1)); context.Response.AppendHeader("ETag", "Never_Modify"); context.Response.Cache.SetETag("Never_Modify"); context.Response.Cache.SetLastModified(DateTime.Now.AddMinutes(-1)); context.Response.TransmitFile(filePath); } else { //断点续传以及多线程下载支持 string[] file_range = range.Substring(6).Split(new char[1] { '-' }); context.Response.Status = "206 Partial Content"; context.Response.AppendHeader("Content-Range", "bytes " + file_range[0] + "-" + file_range[1] + "/" + fileSize.ToString()); context.Response.AppendHeader("Content-Length", (Int32.Parse(file_range[1]) - Int32.Parse(file_range[0]) + 1).ToString()); context.Response.TransmitFile(filePath, long.Parse(file_range[0]), (long)(Int32.Parse(file_range[1]) - Int32.Parse(file_range[0]) + 1)); } } catch (Exception exp) { JsonItem rv = new JsonItem(); rv.Attributes.Add("success", false); rv.Attributes.Add("errorMessage", exp.Message); context.Response.Write(rv.ToString()); } }
public void ProcessRequest(HttpContext context) { try { string idstr = context.Request.Params["fileids"]; JsonItem rv = new JsonItem(); rv.Attributes.Add("success", true); JsonItemCollection files = new JsonItemCollection(); rv.Attributes["files"] = files; if (String.IsNullOrEmpty(idstr)) { context.Response.Write(rv.ToString()); return; } string[] ids = idstr.Split(',', ';'); AttachmentCollection attachments; using (IDbConnection cn = QueryManager.CurrentProvider.OpenConnection()) { attachments = YZAttachmentHelper.GetAttachmentsInfo(cn, ids); } foreach (Attachment attachment in attachments) { JsonItem file = new JsonItem(); files.Add(file); file.Attributes["attachment"] = attachment.FileID; file.Attributes["name"] = attachment.Name; file.Attributes["type"] = attachment.Ext; file.Attributes["size"] = attachment.Size; string requestUrl = HttpContext.Current.Request.Url.ToString(); string RedirectURLBase = requestUrl.Substring(0, requestUrl.IndexOf("Attachment", StringComparison.OrdinalIgnoreCase)) + "Attachment/download.ashx?FileID=" + attachment.FileID; file.Attributes["DownloadUrl"] = RedirectURLBase; } 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"; // if (attachments.Count > 0) context.Response.Write(rv.ToString()); } catch (Exception ex) { JsonItem rv = new JsonItem(); rv.Attributes.Add("success", false); rv.Attributes.Add("errorMessage", JosonMobile.Msg_ALL_FileNoExist + ex.Message); context.Response.Write(rv.ToString()); } }