2015-04-29 85 views
-8

我想根據各自的鏈接按鈕在新窗口中打開我上傳的文檔。文檔以完整路徑保存在數據庫中。如何打開鏈接按鈕文檔onClick按

<asp:LinkButton ID="lnkstdres" runat="server" Text='<%#Eval("Alias") %>' CommandArgument='<%#Eval("Idinst") %>' CommandName="StudentRes" OnClick="lnkstdres_Click"></asp:LinkButton> 

protected void lnkstdres_Click(object sender, EventArgs e) 
    { 

    } 

我不知道該怎麼做。 謝謝

回答

0
if((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0)) 
{ 
    string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName); 
    string SaveLocation = Server.MapPath("Data") + "\\" + fn; 
    try 
    { 
     File1.PostedFile.SaveAs(SaveLocation); 
     Response.Write("The file has been uploaded."); 
    } 
    catch (Exception ex) 
    { 
     Response.Write("Error: " + ex.Message); 
     //Note: Exception.Message returns a detailed message that describes the current exception. 
     //For security reasons, we do not recommend that you return Exception.Message to end users in 
     //production environments. It would be better to put a generic error message. 
    } 
} 
else 
{ 
    Response.Write("Please select a file to upload."); 
} 
0

我發佈了我的問題的解決方案。

<asp:GridView ID="grvstdRes" runat="server" CssClass="table table-bordered table-hover" OnRowCommand="grvstdRes_RowCommand" AutoGenerateColumns="false"> 
               <Columns> 
                <asp:TemplateField HeaderText="Document"> 
                 <ItemTemplate> 
                  <asp:LinkButton ID="lnkstdres" runat="server" Text='<%#Eval("Alias") %>' 

CommandArgument='<%#Eval("Idinstance") %>' CommandName="StudentResAliasName" ></asp:LinkButton> 
                  </ItemTemplate> 
                 </asp:TemplateField> 
                </Columns> 
               </asp:GridView> 

    protected void grvstdRes_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     if(e.CommandName=="StudentResAliasName") 
     { 
      using(objResultUnitBLL=new UnitBLL()) 
      { 
       using(objUnitDTO=new UnitDTO()) 
       { 
        objUnitDTO = objResultUnitBLL.GetStudentResourceFile(Convert.ToInt64(e.CommandArgument)); 
        string fileName=objUnitDTO.FileName; //to get the full path from DB 
        string[] pathArr = fileName.Split('\\'); // To split the path 
        fileName = pathArr.Last().ToString(); //To get the file name with extension 
        Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open('../Upload/Instance/" + fileName + "','_newtab');", true); //to display the file in new window. 
       } 
      } 
     } 
    }