Save As with GridHyperLinkColumn

4 posts, 0 answers
  1. Kurt Kluth
    Kurt Kluth avatar
    167 posts
    Member since:
    Oct 2009

    Posted 08 Apr 2015 Link to this post

    What I would like to do is when a user clicks on a GridHyperLinkColumn within the radGrid, the default behavior is to be prompted with the "Save As" dialog.  Thus far I have been unsuccessful in doing this.  Attempted to use the HTML5 download option but GridHyperLinkColumn doesn't understand that.  This is my current code but it doesn't work on every file as some open up.  The file types may be varied (PDF, XLS, XLSX, DOC, DOCX, TXT, ZIP).  Would it be possible to display a RadContextMenu when clicking on the link with "Save As" the only option?

     

    <telerik:GridHyperLinkColumn SortExpression="FileName" DataTextFormatString="{0}"
        DataNavigateUrlFields="file_name" UniqueName="file_name" DataNavigateUrlFormatString="/Repository/FTPCloud/{0}"
        HeaderText="File" DataTextField="file_name">
    </telerik:GridHyperLinkColumn>

     

    Private Sub grdCloudFiles_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles grdCloudFiles.ItemDataBound
        If TypeOf e.Item Is GridDataItem Then
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
            Dim link As HyperLink = DirectCast(item("file_name").Controls(0), HyperLink)
            link.Attributes.Add("onclick", String.Format("javascript:document.execCommand('SaveAs','true','{0}');", MapPath(".") & link.NavigateUrl))
        End If
     
    End Sub

     

  2. Kurt Kluth
    Kurt Kluth avatar
    167 posts
    Member since:
    Oct 2009

    Posted 09 Apr 2015 Link to this post

    Was able to solve my problem and including the code for others.  Hopefully you find it helpful.  What I did was pass the filename into a page called DownloadFile.aspx.

    Dim filename As String = MapPath("~") & "Repository\FTPCloud\" & Request("file").ToString()
     
     Dim extension As String = Path.GetExtension(filename)
     Dim file As System.IO.FileInfo = New System.IO.FileInfo(filename) '-- if the file exists on the server
     If file.Exists Then 'set appropriate headers
         Response.Clear()
     
         Select Case extension
             Case ".txt"
                 Response.ContentType = "text/plain"
                 Response.AddHeader("content-disposition", (Convert.ToString("attachment; filename=") & Path.GetFileName(filename))) ' + ".txt")
             Case ".xls"
                 Response.ContentType = "application/vnd.ms-excel"
                 Response.AddHeader("content-disposition", (Convert.ToString("attachment; filename=") & Path.GetFileName(filename))) ' + ".xls")
             Case ".xlsx"
                 Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                 Response.AddHeader("content-disposition", (Convert.ToString("attachment; filename=") & Path.GetFileName(filename))) ' + ".xlsx")
             Case ".pdf"
                 Response.ContentType = "application/pdf"
                 Response.AddHeader("content-disposition", (Convert.ToString("attachment; filename=") & Path.GetFileName(filename))) ' + ".pdf")
             Case ".zip"
                 Response.ContentType = "application/zip"
                 Response.AddHeader("content-disposition", (Convert.ToString("attachment; filename=") & Path.GetFileName(filename))) ' + ".zip")
             Case ".doc"
                 Response.ContentType = "application/msword"
                 Response.AddHeader("content-disposition", (Convert.ToString("attachment; filename=") & Path.GetFileName(filename))) ' + ".doc")
             Case ".docx"
                 Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                 Response.AddHeader("content-disposition", (Convert.ToString("attachment; filename=") & Path.GetFileName(filename))) ' + ".docx")
         End Select
         'Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
         'Response.AddHeader("Content-Length", file.Length.ToString())
         'Response.ContentType = "application/octet-stream"
         Response.WriteFile(filename)
         Response.End() 'if file does not exist
     Else
         Response.Write("This file does not exist.")
     End If 'nothing in the URL as HTTP GET

  3. Mostafa
    Mostafa avatar
    1 posts
    Member since:
    Apr 2016

    Posted 13 Jul 2018 in reply to Kurt Kluth Link to this post

    Do you have the full source code in C#?
  4. Kurt Kluth
    Kurt Kluth avatar
    167 posts
    Member since:
    Oct 2009

    Posted 16 Jul 2018 in reply to Mostafa Link to this post

    No, we do not have the code in c# but have it in vb.  I suppose you could use the Telerik code converter.
Back to Top