2013-05-15 30 views
2

我想在HyperLink點擊服務器上打開物理文件。使用ASP.NET在服務器上打開物理文件

<asp:HyperLink ID="HyButton1" Target="_blank" NavigateUrl='<%#Eval("FullPath") %>' runat="server" Text="Open File" ></asp:HyperLink> 

「FULLPATH」 就像是 「E:\ PINCDOCS \ Mydoc.pdf」

目前在Chrome中,我發現了錯誤。

不允許加載本地資源:

可以這樣做或任何其他替代解決方案?

+0

這是不可能的。 –

+1

使用linkbutton;爲按鈕添加事件處理程序;在服務器端打開文件(使用'Server.MapPath方法');然後作爲pdf流。更好的是,如果可能的話,將該文件複製到靜態資源目錄中並使用普通超鏈接。 – mshsayem

+0

讓客戶端下載一個你需要一個url的文件,首先你需要將你的服務器本地路徑轉換爲一個url 這裏是一個鏈接來做 http://stackoverflow.com/questions/16007/how-do-i -convert-a-file-path-to-a-url-in-asp-net – Kiarash

回答

0
//SOURCE 
<asp:HyperLink ID="HyButton1" Target="_blank" NavigateUrl='<%#ful_path(Eval("")) %>' runat="server" Text="Open File" ></asp:HyperLink>//ful_path is c# function name 

//C#: 
protected string ful_path(object ob) 
{ 
    string img = @Request.PhysicalApplicationPath/image/...; 
    return img; 
} 
2

物理文件應位於IIS網站,虛擬目錄或Web應用程序中。所以你需要創建一個虛擬目錄到E:\ PINCDOCS。請參閱此處獲取說明:http://support.microsoft.com/kb/172138

然後在您的代碼中,您可以使用如下代碼:http://geekswithblogs.net/AlsLog/archive/2006/08/03/87032.aspx獲取物理文件的Url。

+0

另外,如果你和服務器在同一個網絡上,你可以簡單地使用網絡名稱:ie:\\ servername \ e $ \ PINCDOCS \ Mydoc.pdf,然後您不必混淆w/IIS。 –

0

當您將NavigateUrl設置爲FullPath時,Chrome將會看到訪問站點的用戶計算機的本地鏈接,而不是服務器本身。

所以,你總是需要使URL任何hyberlink是在// someURL的形式或http://someurl

你的情況,你必須刪除NavigateUrl並添加OnClick處理程序,並在裏面處理程序中,你將使用FILESTREAM讀取文件和寫入文件內容響應流然後刷新它的點擊處理程序的

例如:

context.Response.Buffer = false; 
context.Response.ContentType = "the file mime type, ex: application/pdf"; 
string path = "the full path, ex:E:\PINCDOCS"; 

FileInfo file = new FileInfo(path); 
int len = (int)file.Length, bytes; 
context.Response.AppendHeader("content-length", len.ToString()); 
byte[] buffer = new byte[1024]; 
Stream outStream = context.Response.OutputStream; 
using(Stream stream = File.OpenRead(path)) { 
    while (len > 0 && (bytes = 
     stream.Read(buffer, 0, buffer.Length)) > 0) 
    { 
     outStream.Write(buffer, 0, bytes); 
     len -= bytes; 
    } 
} 
0

來源:

<asp:Button id="Button1" Text="open file" OnClick="Button1_Click" runat="server"/> 

C#:

//open file using full path: 
protected void Button1_Click(object sender, EventArgs e) 
{ 
     System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
     proc.StartInfo.FileName = @"E:\PINCDOCS\Mydoc.pdf"; 
     proc.Start(); 
} 

//open file from your current project: 
protected void Button1_Click(object sender, EventArgs e) 
{ 
     System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
     proc.StartInfo.FileName = HttpContext.Current.Server.MapPath("~/Mydoc.pdf"); 
     proc.Start(); 
} 
相關問題