This is a migrated thread and some comments may be shown as answers.

Save As with GridHyperLinkColumn

3 Answers 80 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kurt Kluth
Top achievements
Rank 1
Kurt Kluth asked on 08 Apr 2015, 06:38 PM

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

 

3 Answers, 1 is accepted

Sort by
0
Kurt Kluth
Top achievements
Rank 1
answered on 09 Apr 2015, 04:15 PM

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

0
Mostafa
Top achievements
Rank 1
answered on 13 Jul 2018, 05:05 AM
Do you have the full source code in C#?
0
Kurt Kluth
Top achievements
Rank 1
answered on 16 Jul 2018, 01:22 PM
No, we do not have the code in c# but have it in vb.  I suppose you could use the Telerik code converter.
Tags
Grid
Asked by
Kurt Kluth
Top achievements
Rank 1
Answers by
Kurt Kluth
Top achievements
Rank 1
Mostafa
Top achievements
Rank 1
Share this question
or