62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace Asiastar.NR.Ajax
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string id = context.Request["id"].ToString();//获取资源的编号
System.IO.Stream iStream = null;
byte[] buffer = new Byte[10000];
int length;
long dataToRead;
NRBLL.File bf = new Asiastar.NRBLL.File();
Guid guid = new Guid(id);
if( bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"] != null )//判断数据库路径是否存在
{
string filepath = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"].ToString();//获取资源完整路径 D:\资源文件\600cc139-14cf-448e-9e50-daa972d35e01.jpg
string Oidfilename = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FileNam"].ToString();//旧文件名称
//string filename = System.IO.Path.GetFileName(filepath);//获取文件名称+后缀名 600cc139-14cf-448e-9e50-daa972d35e01.JPG
//int index = filepath.IndexOf(".");
//string filetype = filepath.Substring(index).ToLower();//后缀名
//string newfilename = Oidfilename;
//string filepath1 = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"].ToString().Substring(0,filepath.Length - 8);
try
{
string fileName = HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(Oidfilename));//解码(注意这里2层解码)
Oidfilename = Oidfilename.Replace("+","%20"); //将“+”替换成“空格”
iStream = new System.IO.FileStream(filepath,System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.Read);
dataToRead = iStream.Length;
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition","attachment; filename=" + HttpUtility.UrlEncode(Oidfilename,System.Text.Encoding.UTF8)); //下载的时候下载原来的文件名称
while( dataToRead > 0 )
{
if( context.Response.IsClientConnected )
{
length = iStream.Read(buffer,0,10000);
context.Response.OutputStream.Write(buffer,0,length);
context.Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
dataToRead = -1;
}
}
}
catch( Exception ex )
{
NR.Error.Log.LogType(ex.ToString());
}
finally
{
if( iStream != null )
{
iStream.Close();
}
}
}
else
{
NR.Error.Log.LogType("找不到文件!");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
#region 上传文件到数据库和服务器
public void FN_UpFiles()
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
StringBuilder strMsg = new StringBuilder();
strMsg.Append("上传的文件分别是:<hr color='pink'/>");
try
{
for( int iFile = 0 ; iFile < files.Count ; iFile++ )
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName = "";//定义文件名
string fileExtension = "";
fileName = Path.GetFileName(postedFile.FileName);//得到上传文件的完整名称 即文件名+后缀名
int index = fileName.IndexOf(".");
string FileType = fileName.Substring(index).ToLower();//截取文件后缀名
//FileTypeImg = "../FileTypeimg/" + hz + ".gif";
Guid fileGuid = Guid.NewGuid();//生成新的文件名称 以GUID命名防止文件名相同
string NewFileName = fileGuid.ToString();//新的文件名
NewFileName = NewFileName + FileType;//新的文件名+后缀名
if( postedFile.ContentLength > 2097151 * 1024 )//判断是否大于配置文件中的上传文件大小
{
Page.RegisterStartupScript("提示","<script language='javascript'>alert('对不起您的上传资源过大!');self.opener.location.reload();window.close();</script>");
return;
}
else
{
if( fileName != "" )//如果文件名不为空
{
try
{
//文件虚拟路径
string strpath = System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName;
try
{
NRModel.File model = new NRModel.File();
NRBLL.File bf = new NRBLL.File();
Guid guid1 = Guid.NewGuid();
Guid guid2 = new Guid(FolderId);
Guid guid3 = Guid.NewGuid();
Guid guid4 = Guid.NewGuid();
model.Fileid = guid1;
model.Folderid = guid2;
model.Filepath = strpath;
model.FileNam = fileName;
model.FileSize = postedFile.ContentLength;
model.Decription = TextArea1.InnerText.ToString();
model.CreateOn = DateTime.Now;
model.CreateBy = guid3;
model.ModefyBy = guid4;
if( bf.FN_AddNewRes(model) > 0 )
{
NR.Error.Log.LogType("上传资源" + fileName + "成功!" + "服务器路径:" + strpath);
//保存文件到指定目录(虚拟目录)
postedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName);
Page.RegisterStartupScript("提示","<script language='javascript'>alert('上传成功!');self.opener.location.reload();window.close();</script>");
}
}
catch( Exception ex )
{
NR.Error.Log.LogType(ex.ToString());
}
}
catch( Exception ex )
{
NR.Error.Log.LogType(ex.ToString());
}
}
else
{
Response.Write("上传文件不能为空!");
NR.Error.Log.LogType("文件不能为空!");
}
}
}
}
catch( System.Exception ex )
{
NR.Error.Log.LogType(ex.ToString());
}
}
#endregion