locked
Share multiple storage files' problem RRS feed

  • Question

  • I need to share multiple storage files to share target. Just like the Microsoft Photo app in Windows 8, we can select several pictures and share in charm bar, and share target can receive these storage files.

    My file is not from picker, instead I have an normal array of storage files.

    I found the share target receives storage file items object.

    The problem is how to match the storage file items object with the share source without using multiple file picker. 

    How to finish this contract?

    Thanks


    Sun Bin



    • Edited by Bin.Sun Monday, September 10, 2012 7:03 AM
    Monday, September 10, 2012 6:41 AM

Answers

  • I got it. 

    I did not set the title and description for share sourse


    Sun Bin

    • Marked as answer by Bin.Sun Wednesday, September 12, 2012 1:14 PM
    Wednesday, September 12, 2012 1:14 PM

All replies

  • I'm not sure what you mean by matching the objects. Can you explain in more detail what you are looking for?

    Also see How to share files .

    --Rob

    Tuesday, September 11, 2012 12:09 AM
    Moderator
  • Hi Rob, 

    Thanks for your attention. Here is my javascript code for share source:

    // share
        var dataTransferManager;
    
        function registerForShareContract() {
            dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();
            dataTransferManager.addEventListener("datarequested", shareDataRequested);
        }
    
        function unRegisterForShareContract() {
            dataTransferManager.removeEventListener("datarequested", shareDataRequested);
        }
    
        function shareDataRequested(e) {
            var request = e.request;
            getAllSelectedPhotos().done(function () {
                var photoList = Context.selectedPhotoList;
                if (Context.vender == 'local') {
    
                    // check 3 photos and no more than 5MB here
                    if (photoList.length <= 3) {     // need to check size no more than 5MB 
    
                        var storageFileList = [];
                        for (var i = 0; i < photoList.length; i++) {
                            var photo = photoList[i];
                            var act = vender.getFileObject('local', photo.albumID, photo);
                            act.then(function (file) {
                                storageFileList.push(file);
                                // check 
                                if (i == (photoList.length - 1)) {
                                    request.data.properties.title = 'photo';
                                    request.data.properties.description = "...";
                                    request.data.setStorageItems(storageFileList);
                                }
                            }, function (e) {
                                console.log("getFileObject " + e);
                            });
                        }
    
                    } else {
                        console.log('Selected more than 3 photos or size is more than 5MB.');
                    }
    
    
                } else {
                    var html;
                    for (var i = 0; i < photoList.length; i++) {
                        html += "<p><img src=\"" + photoList[i].originalUrl + "\">.</p>";
                        var streamRef = Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(new Windows.Foundation.Uri(photoList[i].originalUrl));
                        request.data.resourceMap[photoList[i].originalUrl] = streamRef;
                    }
    
                    request.data.properties.title = "Share Photos by Html";
                    request.data.properties.description = "Online photos.";
                    var htmlFormat = Windows.ApplicationModel.DataTransfer.HtmlFormatHelper.createHtmlFormat(html);
                    request.data.setHtmlFormat(htmlFormat);
    
                }
            });
    
    
        }
    

    js code for share target:

    // StorageItems
            if (shareOperation.data.contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.storageItems)) {
                shareOperation.data.getStorageItemsAsync().done(function (storageItems) {
                    var fileList = "", notSupportNames = [];
                    StorageFileList = storageItems;
                    for (var i = 0; i < storageItems.size; i++) {
                        var storageItem = storageItems.getAt(i);
                        if (storageItem.fileType == '.jpg' || storageItem.fileType == '.jpeg' || storageItem.fileType == '.jpe') {
                            fileList += storageItem.name;
                            var img = document.createElement('img');
                            img.src = window.URL.createObjectURL(storageItem);
                            img.className = "sharedPhotoList";
                            var imgArea = $('#imageArea');
                            imgArea.append(img);
                            imgArea.show();
    
                            if (i < storageItems.size - 1) {
                                fileList += ", ";
                            }
                        } else {
                            notSupportNames.push(storageItem.name);
    
                        }
                    }
    
                    //for (var i = 0; i < storageItems.length; i++) {
                    //    var storageItem = storageItems[i];
                    //    if (storageItem.fileType == '.jpg' || storageItem.fileType == '.jpeg' || storageItem.fileType == '.jpe') {
                    //        fileList += storageItem.name;
                    //        if (i < storageItems.length - 1) {
                    //            fileList += ", ";
                    //        }
                    //    } else {
                    //        notSupportNames.push(storageItem.name);
    
                    //    }
                    //}
    
    
                    if (notSupportNames.length) {
                        document.getElementById("shareDiv").style.display = "none";
                        
                        var html = '';
                        for (var i = 0; i < notSupportNames.length; i++) {
                            html += notSupportNames[i] + "<br>";
    
                        }
                        output.innerHTML = html + 'File(s) typle is not supported.';
                    } else {
    
                        displayContent("StorageItems: ", fileList, false);
                        output.innerHTML = "";
                        $('#Upload2Snapfish').show();
                    }
                });
            }

    In share source code, 

    request.data.setStorageItems(storageFileList);
    

    I set an array list to storageItems, but I cannot get storageItems in share target side. Because in share target side, the storageItems is an object, not an array. 


    Sun Bin

    Tuesday, September 11, 2012 12:44 AM
  • I got it. 

    I did not set the title and description for share sourse


    Sun Bin

    • Marked as answer by Bin.Sun Wednesday, September 12, 2012 1:14 PM
    Wednesday, September 12, 2012 1:14 PM