0

此 ImageUpload 功能有效,但每次我上传新照片时,它都会不断添加副本。那我不是我想要的。当我上传第一张图片时,它可以正常工作。一张图片上传到我的服务器。但是当我继续选择或拍摄新照片时,它会上传同一张照片的两张副本。我上传的第三张图片变成了上传树次,接下来是四次,所以每次上传的副本都会增长。只有当我重新启动应用程序时,它才会再次从一个副本上传开始。我希望有人能看到什么是错的,或者我可以在上传过程后如何重置“selectedImage”。

//this variable will hold the image data blob from the device's gallery or camera
    var selectedImage = null;

    var buttonSelectImage = Titanium.UI.createButton({
        width:240,
        height:50,
        top:242,
        title:'Select image'
    });

    buttonSelectImage.addEventListener('click', function(e){
        dialog.show();
    });

    var buttonUpload = Titanium.UI.createButton({
        width:240,
        height:50,
        top:304,
        title:'Send to site'
    });

    //our dialog with the options of where to get an image from
    var dialog = Titanium.UI.createOptionDialog({
        title: 'Choose source...',
        options: ['Camera','Album','Cansel'],
        cancel:2
    });

    //Dialog event listener
    dialog.addEventListener('click',function(e){
        Ti.API.info('You selected… ' + e.index);
        if(e.index == 0){
        //from the camera
        Titanium.Media.showCamera({
        success:function(event){
        selectedImage = event.media; 
        if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO){
        imageThumbnail.image = selectedImage;
        win.add(imageThumbnail);            
        }
        buttonUpload.addEventListener('click', function(e){
        if(selectedImage != null) {
        postToSite();
        }else{
        alert('You must choose a image!');
        }
        });
        win.add(buttonUpload);
        buttonUpload.show();
        },
        cancel:function(){
        //getting image from camera was cancelled
        },
        error:function(error){
        // create alert
        var a = Titanium.UI.createAlertDialog({title:'Camera'});
        // set message
        if (error.code == Titanium.Media.NO_CAMERA){
        a.setMessage('Your cellphone lack the camera function!');
        }else{
        a.setMessage('Unexpected error: ' + error.code);
        }
        // show alert
        a.show();
        },
        allowImageEditing:true,
        saveToPhotoGallery:true
        });
        }
        else if(e.index == 1){
        //obtain an image from the gallery
        Titanium.Media.openPhotoGallery({
        success:function(event){
        selectedImage = event.media;
        // set image view
        Ti.API.debug('Our type was: '+event.mediaType);
        if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO){
        // set image view
        imageThumbnail.image = selectedImage;
        win.add(imageThumbnail);
        }
        buttonUpload.addEventListener('click', function(e){
        if(selectedImage != null) {
        postToSite();
        }else{
        alert('You must choose a image!');
        }
        });
        win.add(buttonUpload);
        buttonUpload.show();        
        },
        cancel:function(){
        //user cancelled the action fron within the photo gallery
        }
        });
        }else{
        //cancel was tapped user opted not to choose a photo
        }
    });
    win.add(buttonSelectImage);

    function randomString(length,current){
    current = current ? current : '';
    return length ? randomString( --length , "abcdefghiklmnopqrstuvwxyz".charAt( Math.floor( Math.random() * 60 ) ) + current ) : current;
    }
    //Post to server
    function postToSite(){
        //create the httpRequest
        var xhr = Titanium.Network.createHTTPClient();
        xhr.onerror = function(e)
            {
                Ti.UI.createAlertDialog({title:'Error', message:e.error}).show();
                Ti.API.info('IN ERROR ' + e.error);
            };
            xhr.setTimeout(40000);
            
        xhr.onsendstream = function(e){
                
                ind.value = e.progress ;
                Ti.API.info('ONSENDSTREAM - PROGRESS: ' + e.progress);
                ind.show();
            };
                
        // Get the sessionID
        var sessionId = Titanium.App.Properties.getString("sessionId");
        
        //open the httpRequest
        xhr.open('POST','http://example.com/index.php?include=app_support/upload.php&session_id='+sessionId);
            activityIndicator.show();
            buttonUpload.hide();
        xhr.onload = function(){
            //the image upload method has finished
            var json = this.responseText;
            var response = JSON.parse(json);
            if (response.auth == 'false'){
                alert('User validation failed');
            }
            else if (response.upload_status == 'success'){
                ind.hide();
                activityIndicator.hide();
                buttonUpload.hide();
                alert(response.message);
            }else{
                ind.hide();
                activityIndicator.hide();
                buttonUpload.show();
                alert(response.message);
            }
        };
        //send the data
        var r = randomString(5) + '.jpg';
        xhr.send({'form_data[file][value]': selectedImage, 'value': r});
    }
4

1 回答 1

0

这可能是您的问题的根源:

dialog.addEventListener('click',function(e) {
...
  buttonUpload.addEventListener('click', function(e){

您正在某些单击事件中添加单击侦听器。因此,每次执行第一次点击事件时,都会在 buttonUpload 中添加另一个点击事件,并执行与调用第一次点击事件一样多的次数。

于 2012-08-13T08:33:50.730 回答