2016-09-20 116 views
0

我上傳的文件,並存儲在數據文件夾中的服務器如何使用asp.net下載pdf文件?

文件路徑:

Project Name 
|_bin 
|_css 
|_Data 
    |_Mohamedfaisal.pdf 

這是我的asp.net代碼

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.IO; 
using System.Data; 

namespace Expatriates { 
public partial class FileUploadForm: System.Web.UI.Page { 
    protected void Page_Load(object sender, EventArgs e) { 

    } 

    protected void Button1_Click(object sender, EventArgs e) { 
    if (FileUpload1.HasFile) { 
    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Data/") + FileUpload1.FileName); 
    } 

    DataTable dt = new DataTable(); 
    dt.Columns.Add("File", typeof(string)); 
    dt.Columns.Add("size", typeof(string)); 
    dt.Columns.Add("type", typeof(string)); 

    foreach(string strFile in Directory.GetFiles(Server.MapPath("~/Data/"))) { 
    FileInfo fi = new FileInfo(strFile); 

    dt.Rows.Add(fi.Name, fi.Length, fi.Extension); 
    } 

    GridView1.DataSource = dt; 
    GridView1.DataBind(); 
    } 

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { 
    if (e.CommandName == "Download") { 
    Response.Clear(); 
    Response.Write(e.CommandArgument); 
    Response.ContentType = "application/octect-stream"; 
    Response.AppendHeader("content-disposition", "filename=" + e.CommandArgument); 
    Response.TransmitFile(Server.MapPath("~/Data/") + e.CommandArgument); // error occured 
    Response.End(); 
    } 
    } 
} 
} 

前端asp.net

<%@ Page Language="C#" AutoEventWireup="true"  CodeBehind="FileUploadForm.aspx.cs" Inherits="Expatriates.FileUploadForm" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div style="font-family:Arial;"> 
     <asp:FileUpload ID="FileUpload1" runat="server" /> 
     <asp:Button ID="Button1" runat="server" Text="Upload"  OnClick="Button1_Click" /> 
     <br /> 
     <br /> 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand"> 
      <Columns> 
       <asp:TemplateField HeaderText="File"> 
        <ItemTemplate> 
         <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" Text='<%# Eval("File") %>'></asp:LinkButton> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:BoundField DataField="Size" HeaderText="Size in Bytes" /> 
       <asp:BoundField DataField="Type" HeaderText="File Type" /> 
      </Columns> 
     </asp:GridView> 
    </div> 
    </form> 
</body> 
</html> 

我的前端設計 UI design

,當我點擊鏈接它表明以下錯誤消息

型「System.IO.DirectoryNotFoundException」的例外在mscorlib.dll發生 但在用戶代碼中沒有處理

附加信息:無法找到路徑'F:\ Visual Studio Project \ Expatriates \ Expatriates \ Data \'的一部分。

上傳文件工作正常,但我沒有得到下載。

+1

用'Path.Combine嘗試(System.Web.HttpContext.Current.Server.MapPath( 「〜/數據/」)); ' –

+0

您是否調試過並檢查e.CommandArgument中的文件名是否存在? – sachin

+0

@sachin:你是對的。它沒有文件名。現在問題修復了。謝謝 –

回答

1

你的代碼可能會失敗,如果e.CommandArgument沒有一個有效的文件名是唯一的原因。我認爲命令參數由於某種原因沒有通過,請查看您的標記。

你必須明確地指定CommandArgumentLinkButton這樣的:

CommandArgument='<%# Eval("File") %>' 
0

您不爲RowCommand事件提供commandArgument。更改

<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" Text='<%# Eval("File") %>'></asp:LinkButton> 

<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" CommandArgument='<%# Eval("File") %>' Text='<%# Eval("File") %>'></asp:LinkButton>