Iron Speed Technical Forums
Register Latest Topics
 
 
 


Reply
  Author   Comment  
sr88595

Registered:
Posts: 15
Reply with quote  #1 
I have a mobile form with a database field as a link. I have it as a link because it appears that ISD does not allow the use of the download control on mobile pages.

How can I get the link to actually download a file on a mobile device?

I know this can be done because I have downloaded files on my Android phone from other websites.

I tried making the file a URL link as shown in the code below but this does not work when testing locally on my PC. I did not test on Android phone, as I assume both will work once I get the code right. I am guessing that the ModifyRedirectUrl method, generated by ISD, needs to know more information such as the file type, Response.ContentType = "application/pdf" etc, and maybe some other information.

Any help would be appreciated.

The code snippet below is from the TableControlRow of the document table.

doc_desc is the link field on the form. This is the link that should download the file when clicked.
doc_desc_Click is executed when the doc_desc link control is clicked on.
The variable doc_location is a hidden field on the form, test value "docs/2015"
The variable doc_filename is a hidden field on the form, test value "somefile.pdf"

Code:
        
         public override void doc_desc_Click(object sender, EventArgs args)
        {
        
            // The redirect URL is set on the Properties, Custom Properties or Actions.
            // The ModifyRedirectURL call resolves the parameters before the
            // Response.Redirect redirects the page to the URL.  
            // Any code after the Response.Redirect call will not be executed, since the page is
            // redirected to the URL.
            
          string url = @"../" + this.doc_location.Text + @"/" + this.doc_filename.Text;
        
        bool shouldRedirect = true;
        string TargetKey = null;
        string DFKA = TargetKey;
        string id = DFKA;
        string value = id;
      
            try {
                // Enclose all database retrieval/update code within a Transaction boundary
                DbUtils.StartTransaction();
                
                url = this.ModifyRedirectUrl(url, "",false);
                url = this.Page.ModifyRedirectUrl(url, "",false);
              
            } catch (Exception ex) {
                  // Upon error, rollback the transaction
                  this.Page.RollBackTransaction(sender);
                  shouldRedirect = false;
                  this.Page.ErrorOnPage = true;

            // Report the error message to the end user
            BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this, "BUTTON_CLICK_MESSAGE", ex.Message);
    
            } finally {
                DbUtils.EndTransaction();
            }
            if (shouldRedirect) {
                this.Page.ShouldSaveControlsToSession = true;
                this.Page.Response.Redirect(url);
            }
        
            else if (TargetKey != null && !shouldRedirect){
            this.Page.ShouldSaveControlsToSession = true ;
            this.Page.CloseWindow(true);
            }
        }
jadach

Registered:
Posts: 203
Reply with quote  #2 
I've had success with something like this:

Create a generic handler called DBfiles.ashx

<%@ WebHandler Language="C#" class="MapHandler" %>
 
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public class MapHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {

        string connectionString = ConfigurationManager.ConnectionStrings["YourConnectionStringName"].ConnectionString;

        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("SELECT FileID, FileUpload, FileName FROM dbo.DatabaseTable WHERE FileID = @ID", connection);
        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = context.Request.QueryString["ID"];
        try
        {
            connection.Open();
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.Default);
           
            if (reader.HasRows)
               
            {
                while (reader.Read())
                {
                    context.Response.ContentType = "application/pdf";

                    context.Response.BinaryWrite((byte[])reader["FileUpload"]);
                }
            }
        }
        finally
        {
            connection.Close();
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

Your Hyperlink can look something like this:
 <a href="DBfiles.ashx?ID=<%#Eval("ID")%>" target="_blank">Download PDF</a>


__________________
Jerry
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.

Download Iron Speed Designer

Terms of Service Privacy Statement