using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using InfoEyeChinatest.Upload;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
namespace InfoEyeChinatest.dbhelper
{
public class infoupload : Page
{
#region 上传文件
/// 文件上传存储方式,StoreType.DataBase为存储在数据库表中,StoreType.FileSystem为存储在磁盘中,默认为StoreType.FileSystem
StoreType Store = StoreType.FileSystem;
/// <summary>
/// 上传附件
/// </summary>
/// <param name="m_files">上传控件</param>
protected void DoUpload(FileUpload m_files)
{
HttpPostedFile pFile = m_files.PostedFile;
if (pFile != null)
{
IList<FileObj> list = new List<FileObj>();
string filePath = pFile.FileName;
string fileExtend = "";
if (filePath.LastIndexOf(".") >= 0)
{
fileExtend = filePath.Substring(filePath.LastIndexOf(".")).ToLower().Trim();
}
string fileName = Path.GetFileName(filePath);
string strPath = "";
string Savepath = Server.MapPath("~/UploadFiles/root/");//上传文件存到的系统物理地址
strPath = Savepath + fileName;
pFile.SaveAs(strPath);
string AID = Guid.NewGuid().ToString();
//获取附件信息
FileObj f = new FileObj();
f.AID = AID;
f.Category = "";
//f.Description = txtDescription.Text;
//上传后附件的存储处理
if (Store == StoreType.DataBase)
{
//附件上传过程中,附件直接写入磁盘中,从磁盘中读取附件内容
byte[] buf;
FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);
buf = new byte[Convert.ToInt32(pFile.ContentLength)];
fs.Read(buf, 0, Convert.ToInt32(pFile.ContentLength));
fs.Close();
fs.Dispose();
if (File.Exists(strPath))
{
File.Delete(strPath);
}
f.FileContent = buf;
}
else
{
f.FilePath = strPath;
}
f.FileExtend = fileExtend;
f.FileName = fileName;
f.FileSize = (int)(pFile.ContentLength / 1024);
f.MimeType = pFile.ContentType;
f.OwnerId = "2";
f.Status = "status";
f.Title = "title";
f.UserId = "userid";
f.UserName = "username";
try
{
f.SortIndex = 0;
}
catch
{
f.SortIndex = 0;
}
//AIDS.Add(f.AID);
list.Add(f);
//flagCancel = false;
FileObjDA fileObjDA = new FileObjDA("ConnectionString");
fileObjDA.Insert(list);
}
}
#endregion
#region 下载文件
/// <summary>
/// 下载附件
/// </summary>
/// <param name="filename">文件名</param>
protected void DoDownload(String filename)
{
String FullFileName = "";
FullFileName = System.Web.HttpContext.Current.Server.MapPath("~/UploadFiles/root/" + filename);//这里是你文件在项目中的位置,根目录下就这么写
FileInfo DownloadFile = new FileInfo(FullFileName);
//DownloadFile.Name = "asdasd";
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
#endregion
#region 删除附件
/// <summary>
/// 删除附件
/// </summary>
/// <param name="filename">文件名</param>
protected void DoDelete(string filename)
{
String path = "";
path = System.Web.HttpContext.Current.Server.MapPath("~/UploadFiles/root/" + filename);//这里是你文件在项目中的位置,根目录下就这么写
if (File.Exists(path))
{
File.Delete(path);
}
}
#endregion
}
}