webservce附件下载

using System;
using System.IO;
using System.Text;
using System.Web;
using System.Data;
using Register.DataAccess;
using System.Configuration;
using HrisInterFaceProject.Helper;
using System.Data.SqlClient;

namespace HrisInterFaceProject.PAGE
{
public partial class AttachDownload : System.Web.UI.Page
{

protected override void OnLoad(EventArgs e)
{
base.Response.CacheControl = "public";
Page_Load(this, e);
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
string logtitle = "期货从业资格传rpa附件下载";
string FileName = string.Empty;
string attachid = Request["attachid"] as string;
string fileid = Request["fileId"] as string;
//string ServerPath = "\\\\10.29.100.78\\vol_nfs_fcsjdkfcs\\附件\\";
string ServerPath = ConfigurationManager.AppSettings["filepath"].ToString();

try
{

if (string.IsNullOrWhiteSpace(attachid) && string.IsNullOrWhiteSpace(fileid))
{
Response.Write(@"<script>alert('未传正确参数!'); </script>");
}
else
{
string type = string.Empty;
string filetype = string.Empty;
bool flag = false;
string fname = string.Empty;
if (!string.IsNullOrWhiteSpace(attachid))
{
string sql = $@"select sourcetype,title,filetype from skyDataFiles where id={attachid}";
DataSet ds = new DataSet();
ds = DbHelperSQL.Query(sql);
type = ds.Tables[0].Rows[0]["sourcetype"].ToString();
filetype = ds.Tables[0].Rows[0]["filetype"].ToString();
FileName = ServerPath + "/" + attachid + "_" + ds.Tables[0].Rows[0]["title"].ToString();
fname= ds.Tables[0].Rows[0]["title"].ToString();
flag = true;

}
else
{
string sql = $@"select sourcetype,title,filetype from skyDataFiles where id={fileid}";
DataSet ds = new DataSet();
ds = DbHelperSQL.Query(sql);
type = ds.Tables[0].Rows[0]["sourcetype"].ToString();
filetype = ds.Tables[0].Rows[0]["filetype"].ToString();
fname = ds.Tables[0].Rows[0]["title"].ToString();
FileName = ServerPath + "/" + fileid + "_" + ds.Tables[0].Rows[0]["title"].ToString();

}


if (type == "1")
{
FileStream fs = new FileStream(FileName, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fname, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
else
{

Byte[] blob = null;

using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
{
string blobsql = string.Empty;
conn.Open();
if (flag)
{
blobsql = $@"select layout from skyDataFiles where sourcetype=0 and id={attachid}";

}
else
{
blobsql = $@"select layout from skyDataFiles where sourcetype=0 and id={fileid}";
}
NLogHelper.WriteLogByCustom(logtitle, blobsql.ToString(), LogLevel.Debug, LogModelPrefix.DNJK, logtitle);
SqlCommand cmd = new SqlCommand(blobsql, conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
blob = new Byte[(sdr.GetBytes(0, 0, null, 0, int.MaxValue))];
sdr.GetBytes(0, 0, blob, 0, blob.Length);

sdr.Close();
conn.Close();
}
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开

Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fname, System.Text.Encoding.UTF8) + "." + HttpUtility.UrlEncode(filetype, System.Text.Encoding.UTF8));
Response.BinaryWrite(blob);
Response.Flush();
Response.End();

 

}


}
} catch (Exception ex)
{
NLogHelper.WriteLogByCustom(logtitle, "错误代码:8000012。下载接口出现问题,请重试或联系管理员检查!" + ex, LogLevel.Error, LogModelPrefix.DNJK, logtitle);

}

}

}

}

posted @ 2023-03-08 08:48  泉城余文乐  Views(15)  Comments(0Edit  收藏  举报