Quantcast
Viewing all articles
Browse latest Browse all 8068

HTTP error when I try to download a remote file onto a device

Hello.

We're currently planning an iOS app that will need to download a number of files onto an iPad from our remote shared server. I'm not a hugely experienced Appcelerator user with just a couple of small projects under my belt, and have not used the 'Titanium.Network.createHTTPClient' functionality either, so I pulled a few lines of code from Kitchen Sink into my dummy project in order to get my head around the whole process.

The Kitchen Sink code (obviously) worked and I (thought that I) understand the syntax, but when I replace the url from the Kitchen Sink target ('http://titanium-studio.s3.amazonaws.com/latest/Titanium_Studio.exe') to a url pointing to a sample file on our shared remote server ('http://www.freehanddev.co.uk/titanium/test_image.jpg'), the onerror function returns an HTTP error. The url is fine as when I enter that into a browser I can access the file successfully.

Can someone explain what I'm doing wrong please, it seems so simple but has me baffled.

/Application Window Component Constructor
function ApplicationWindow() {
    //load component dependencies
    var FirstView = require('ui/common/FirstView');
 
    //create component instance
    var self = Ti.UI.createWindow({
        title : 'Download Page'
    }), isAndroid = Ti.Platform.name === 'android', isTizen = Titanium.Platform.osname === 'tizen', ind = Titanium.UI.createProgressBar({
        width : 200,
        height : 50,
        min : 0,
        max : 1,
        value : 0,
        top : 10,
 
        // iOS can display PDFs, Android can display PNGs, but Tizen can display neither, because
        // it's MobileWeb-based, and MobileWeb can neither show PDFs nor reliably work with binary
        // content. Therefore, Tizen will download and show HTML.
        message : 'Downloading ' + (isAndroid || isTizen ? 'PNG' : 'PDF') + ' File',
        font : {
            fontSize : 12,
            fontWeight : 'bold'
        },
        color : '#888'
    });
 
    Ti.Platform.name === 'iPhone' && (ind.style = Titanium.UI.iPhone.ProgressBarStyle.PLAIN);
 
    self.add(ind);
    ind.show();
 
    //construct UI
    var firstView = new FirstView();
    self.add(firstView);
 
    var largeFile = Titanium.UI.createButton({
        title : 'Large File Download',
        height : 40,
        width : 200,
        top : 220
    });
    self.add(largeFile);
 
    largeFile.addEventListener('click', function() {
        ind.value = 0;
        c = Titanium.Network.createHTTPClient();
        c.setTimeout(10000);
        c.onload = function(e) {
            Ti.API.info("ONLOAD = " + e);
        };
        c.ondatastream = function(e) {
            ind.value = e.progress;
            Ti.API.info('ONDATASTREAM1 - PROGRESS: ' + e.progress);
        };
        c.onerror = function(e) {
            Ti.UI.createAlertDialog({
                title : 'XHR',
                message : 'Error: ' + e.error
            }).show();
        };
 
               // this is my url which results in an http error
         c.open('GET','http://www.freehanddev.co.uk/titanium/test_image.jpg');
 
               // this is the url from kitchen sink which works
                c.open('GET', 'http://titanium-studio.s3.amazonaws.com/latest/Titanium_Studio.exe');
        ind.message = 'Downloading large file';
        if (isTizen) {
            // Property "file" is a path to a file. It is not an object of the type "File".
            // See documentation about Titanium.Network.HTTPClient
            c.file = 'tiStudio.exe';
        } else {
            // c.file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'CP_D1_session02.mp4');
        }
        c.send();
    });
 
    return self;
}
 
//make constructor function the public component interface
module.exports = ApplicationWindow;
Thank you.

Phil

Titanium Command-Line Interface, CLI version 3.2.3, Titanium SDK version 3.2.2.GA App is for iOS (iPad) and Titanium is running on a Mac under OSX. Testing on iOS Simulator Titanium Studio, build: 3.2.3.201404181442


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>