/// <summary> /// 得到源文件的大小,通过 FileInfo对象获取 /// </summary> /// <param name="serverFileName">文件名</param> /// <param name="serverFolderName">文件夹名</param> /// <param name="fileType">根据fileType得到是属于什么类型的资源</param> /// <param name="previewType"></param> /// <returns></returns> public static long GetResourceFileSize(string serverFileName, string serverFolderName, string fileType, string previewType) { long l = 0; string resourceRootPath = string.Empty; string filePath = string.Empty; IResourceType obj = ResourceTypeFactory.getResourceType(fileType); //yangguang //filePath = Path.Combine(obj.SourcePath, serverFolderName); //filePath = Path.Combine(filePath, serverFileName); filePath = obj.GetSourcePath(serverFolderName, serverFileName); if (File.Exists(filePath)) { try { l = new FileInfo(filePath).Length; } catch (Exception ep) { LogWriter.WriteExceptionLog(ep, true); } } return(l); }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string ids = Request.QueryString["ids"]; if (string.IsNullOrEmpty(ids)) { return; } ids = ids.TrimEnd(";".ToCharArray()); string[] arrIds = ids.Split(";".ToCharArray()); ImageType objImageType = new ImageType(); IResourceType rt = null; string physicalPath = string.Empty; List <string> zipFileList = new List <string>(); foreach (string id in arrIds) { ResourceEntity re = null; Resource r = new Resource(); re = r.GetResourceInfoByItemId(id); //physicalPath = objImageType.GetSourcePath(re.FolderName, re.ServerFileName); rt = ResourceTypeFactory.getResourceTypeByString(re.ResourceType); physicalPath = rt.GetSourcePath(re.FolderName, re.ServerFileName); if (!string.IsNullOrEmpty(physicalPath)) { zipFileList.Add(physicalPath); } //记录下载日志 Resource.Production_Hires_Down_Log(re.ItemSerialNum, Path.GetExtension(re.ServerFileName), CurrentUser.UserName, "", "", re.FolderName, false, re.ResourceType); } Response.ContentType = "application/octet-stream"; Response.AppendHeader("content-disposition", "attachment; filename=\"Download.zip\""); Response.CacheControl = "Private"; Response.Cache.SetExpires(DateTime.Now.AddMinutes(3)); byte[] buffer = new byte[4096]; ZipOutputStream zipOutputStream = new ZipOutputStream(Response.OutputStream); zipOutputStream.SetLevel(3); foreach (string fileName in zipFileList) { Stream fs = File.OpenRead(fileName); // or any suitable inputstream ZipEntry entry = new ZipEntry(ZipEntry.CleanName(Path.GetFileName(fileName))); entry.Size = fs.Length; zipOutputStream.PutNextEntry(entry); int count = fs.Read(buffer, 0, buffer.Length); while (count > 0) { zipOutputStream.Write(buffer, 0, count); count = fs.Read(buffer, 0, buffer.Length); if (!Response.IsClientConnected) { break; } Response.Flush(); } fs.Close(); } zipOutputStream.Close(); Response.Flush(); Response.End(); } }
/// <summary> /// 进行请求处理,返回图像或数据 /// </summary> private void ProcessRequest(string fileName, string subfolder, string fileType, string userName, string usage, string endUser, string resourceType, string attType) { //string strResourceRootPath = UIBiz.CommonInfo.ResourceRootPath; strUserName = CurrentUser.UserLoginName; string strFullFilePath = string.Empty; IResourceType rt = null; if (string.IsNullOrEmpty(subfolder)) { rt = ResourceTypeFactory.getResourceTypeByString(resourceType.ToLower()); //yangguang //strFullFilePath = Path.Combine(rt.SourcePath, userName); //strFullFilePath = Path.Combine(strFullFilePath, fileName + fileType); strFullFilePath = rt.GetSourcePath(userName, fileName + fileType); strFile = resourceType.ToLower() + "_" + strFullFilePath; } else { if (resourceType.ToLower().Equals("image")) { rt = ResourceTypeFactory.getResourceTypeByString(resourceType.ToLower()); //yangguang //strFullFilePath = Path.Combine(rt.SourcePath, userName); //strFullFilePath = Path.Combine(strFullFilePath, subfolder + "/" + fileName + fileType); strFullFilePath = rt.GetSourcePath(userName, subfolder + "/" + fileName + fileType); strFile = resourceType.ToLower() + "_" + strFullFilePath; } else if (resourceType.ToLower().Equals("video")) { rt = ResourceTypeFactory.getResourceTypeByString(resourceType.ToLower()); //yangguang //strFullFilePath = Path.Combine(rt.SourcePath, userName); //strFullFilePath = Path.Combine(strFullFilePath, subfolder + "/" + fileName + fileType); strFullFilePath = rt.GetSourcePath(userName, subfolder + "/" + fileName + fileType); strFile = resourceType.ToLower() + "_" + strFullFilePath; } else if (resourceType.ToLower().Equals("other")) { rt = ResourceTypeFactory.getResourceTypeByString(resourceType.ToLower()); //yangguang //strFullFilePath = Path.Combine(rt.SourcePath, userName); //strFullFilePath = Path.Combine(strFullFilePath, subfolder + "/" + fileName + fileType); strFullFilePath = rt.GetSourcePath(userName, subfolder + "/" + fileName + fileType); strFile = resourceType.ToLower() + "_" + strFullFilePath; } } #region //string resUrl = string.Empty; //resUrl = UIBiz.CommonInfo.FileDownPath + "/" + userName + "{0}" + fileName + fileType; //if (resourceType.ToLower().Equals("video")) //{ // strFullFilePath = Path.Combine(Path.Combine(Path.Combine(strResourceRootPath, "video"), "Source"), userName); // if (attType.ToLower().Equals("attachment")) // { // resUrl = UIBiz.CommonInfo.FileDownPath + "/../video/source/" + userName + "{0}/attachments/" + fileName + fileType; // strFullFilePath = Path.Combine(strFullFilePath, UIBiz.CommonInfo.AttachFolder); // } //} //else if (resourceType.ToLower().Equals("image")) //{ // //strFullFilePath = Path.Combine(Path.Combine(Path.Combine(strResourceRootPath,"images"), "Source") , userName); //91的结构 // strFullFilePath = Path.Combine(UIBiz.CommonInfo.ImageRootPath, userName); // if (attType.ToLower().Equals("attachment")) // { // resUrl = UIBiz.CommonInfo.FileDownPath + "/../video/source/" + userName + "{0}/attachments/" + fileName + fileType; // strFullFilePath = Path.Combine(strFullFilePath, UIBiz.CommonInfo.AttachFolder); // } //} //else if (resourceType.ToLower().Equals("document")) //{ // strFullFilePath = Path.Combine(Path.Combine(strResourceRootPath, "document"), userName); //} //else if (resourceType.ToLower().Equals("other")) //{ // strFullFilePath = Path.Combine(Path.Combine(strResourceRootPath, "other"), userName); //} #endregion if (File.Exists(strFullFilePath)) { WriteLog wl = new WriteLog(this.WriteLogDB); wl.BeginInvoke(fileName, fileType, strUserName, usage, endUser, userName, true, resourceType, null, null); System.IO.Stream iStream = null; try { byte[] buffer = new Byte[10000]; int length; long dataToRead; string filepath = strFullFilePath; string filename = System.IO.Path.GetFileName(filepath); iStream = new System.IO.FileStream(strFullFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read); dataToRead = iStream.Length; Response.Buffer = false; Response.AddHeader("Connection", "Keep-Alive"); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + encodeChineseFileName(filename)); Response.AddHeader("Content-Length", iStream.Length.ToString()); while (dataToRead > 0) { if (Response.IsClientConnected) { length = iStream.Read(buffer, 0, 10000); Response.OutputStream.Write(buffer, 0, length); Response.Flush(); buffer = new Byte[10000]; dataToRead = dataToRead - length; } else { dataToRead = -1; } } } catch (Exception ex) { // Trap the error, if any. Response.Write("Error : " + ex.Message); } finally { if (iStream != null) { //Close the file. iStream.Close(); } } #region //Response.WriteFile(strFullFilePath); //Response.TransmitFile(strFullFilePath); //Response.Clear(); //Response.Buffer = true; //Response.ContentType = "application/octet-stream"; //Response.AddHeader("Content-Disposition", "attachment;filename=" + encodeChineseFileName(fileName + fileType)); //Response.BinaryWrite(rawImg); //Response.End(); #endregion } #region //if (!string.IsNullOrEmpty(subfolder)) //{ // resUrl = string.Format(resUrl, "/" + subfolder + "/"); //} //else //{ // resUrl = string.Format(resUrl, "/"); //} //if (resUrl != null && resUrl != "") //{ // // wl.BeginInvoke(fileName, fileType, CurrentUser.UserLoginName, usage, endUser, userName, true , resourceType, null, null); // //Response.Redirect(resUrl, true); // //这里不采用 response.binarywrite方法往客户端发送数据,主要是大文件引起的问题,而且也没有这个必要 // //by ciqq 2010-4-1 //} #endregion }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string lightboxId = Request.QueryString["id"]; if (!string.IsNullOrEmpty(lightboxId)) { //得到该收藏夹下的图片 Resource obj = new Resource(); DataSet ds = obj.GetResourcesByLightBoxID(new Guid(lightboxId), int.MaxValue, 1); if (ds.Tables[0].Rows.Count > 0) { ImageType objImageType = new ImageType(); IResourceType rt = null; string physicalPath = string.Empty; List <string> zipFileList = new List <string>(); foreach (DataRow dr in ds.Tables[0].Rows) { rt = ResourceTypeFactory.getResourceTypeByString(dr["ResourceType"].ToString().ToLower()); physicalPath = rt.GetSourcePath(dr["ServerFolderName"].ToString(), dr["ServerFileName"].ToString()); //physicalPath = objImageType.GetSourcePath(dr["ServerFolderName"].ToString(), dr["ServerFileName"].ToString()); if (!string.IsNullOrEmpty(physicalPath)) { zipFileList.Add(physicalPath); } } Response.ContentType = "application/octet-stream"; Response.AppendHeader("content-disposition", "attachment; filename=\"Download.zip\""); Response.CacheControl = "Private"; Response.Cache.SetExpires(DateTime.Now.AddMinutes(3)); byte[] buffer = new byte[4096]; ZipOutputStream zipOutputStream = new ZipOutputStream(Response.OutputStream); zipOutputStream.SetLevel(3); foreach (string fileName in zipFileList) { Stream fs = File.OpenRead(fileName); // or any suitable inputstream ZipEntry entry = new ZipEntry(ZipEntry.CleanName(Path.GetFileName(fileName))); entry.Size = fs.Length; zipOutputStream.PutNextEntry(entry); int count = fs.Read(buffer, 0, buffer.Length); while (count > 0) { zipOutputStream.Write(buffer, 0, count); count = fs.Read(buffer, 0, buffer.Length); if (!Response.IsClientConnected) { break; } Response.Flush(); } fs.Close(); } zipOutputStream.Close(); Response.Flush(); Response.End(); } } } }