码迷,mamicode.com
首页 > Windows程序 > 详细

c#从服务器下载文件

时间:2016-05-05 16:05:53      阅读:371      评论:0      收藏:0      [点我收藏+]

标签:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="library_details.aspx.cs"     Inherits="AttenceWeb.WorldHome.library_details" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">  

   <title>文件下载</title>    

    <script src="Scripts/jquery-1.8.3.min.js" type="text/javascript"></script>

 </head>

<body>

<form id="form1" runat="server">
    <table border="0" cellpadding="0" cellspacing="0" width="80%">

<tr height="30">
<td class="td_title">
                        文件下载:
                    </td>
                    <td id="tdfile" colspan="3">
                        <span id="doowntimes" runat="server"></span><a href="#" onclick="downFile();">下载
                        </a>
                    </td>
                </tr>

</table>
    <iframe id="exportIFrame" style="display: none;"></iframe>
    <input type="hidden" value="" id="hidNewId" runat="server" />
    </form>

</Body>

</html>

<script type="text/jscript">

//下载文件
    function downFile() {
        var newsid = $("#hidNewId").val();
            //附件下载
            var url = "downloadfile.aspx?newsid=" + newsid;
            var exportIFrame = document.getElementById("exportIFrame");
            exportIFrame.src = url;
    }

</script>

 

 

 

 

 

 

 

 

 

 

 

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="downloadfile.aspx.cs" Inherits="AttenceWeb.WorldHome.downloadfile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">    

<title></title>

</head>

<body>    

<form id="form1" runat="server">    

<div>         </div>    

</form>

</body>

</html>

 

 

using System;

using System.Collections.Generic;

using System.Data;

using System.IO;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Attence.Logic;

using CommonBaseV1_0;

namespace AttenceWeb.WorldHome {    

public partial class downloadfile : System.Web.UI.Page    

{        

              Sys_LibraryFileBLL bll = new Sys_LibraryFileBLL();

              ReturnObj rObj = new ReturnObj();        

              protected void Page_Load(object sender, EventArgs e)        

             {            

                  if (!IsPostBack)            

                  {

                         string id = Context.Request["newsid"];                

                          //根据附件ID获取附件信息  

                         rObj = bll.GetFileByNewsId(id);                

                       if (rObj.IsSuccess)   

                       {   

                             taTable result = rObj.ObjValue as DataTable;                    

                             if (result.Rows.Count > 0)                    

                         {

                       //路径                        

                       sing fname = result.Rows[0]["FilePath"].ToString();                        

                        //文件名                        

                       string title = result.Rows[0]["FileName"].ToString();                        

                         string path = fname + title;   

//获取当前正在执行的服务器应用程序的根目录的物理文件系统路径

 //string tempFile = Request.PhysicalApplicationPath;
                      //string dataDir = tempFile + "UploadFiles\\";

if (File.Exists(path))                        

{                            

try                            

{                                

System.IO.FileInfo file = new System.IO.FileInfo(path);                                

Response.Clear();                                

Response.ClearHeaders();                                

Response.Buffer = false;                                

Response.ContentType = "application/octet-stream";                                

Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(title, System.Text.Encoding.UTF8));                                

Response.AppendHeader("Content-Length", file.Length.ToString());                       

Response.WriteFile(file.FullName);                                

Response.Flush();                                

HttpContext.Current.ApplicationInstance.CompleteRequest();                            

}                            

catch (Exception ex)                            

{                                

Response.Write(ex.ToString());                            

}                        

}else{

 Response.Write("文件不存在“);

}

                   

}                

}            

}        

}

   

}

}

 

c#从服务器下载文件

标签:

原文地址:http://www.cnblogs.com/lk516924/p/5462058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!