locked
Handle error when save file to the storage

    คำถาม

  • Hi,

    How can we know the description or type of error when we fail to save file on the storage? Do we have any documents about that? My application have to save a lot of images to cache. I need to check when cache is full, then delete the old data. But I don't want to count the size of storage to check because it maybe  slow. So I want to check the error if we fail to save file. And if error caused by not enough space to save, I will delete data. Can we do that?

    Thanks,

    Vivian

    13 มีนาคม 2555 9:29

คำตอบ

  • Hi Vivian,

    We also provide ways to determine the amount of available free space:

    System.FreeSpace can be retrieved with StorageItem.retrievePropertiesAsync and gives you the amount of free space for the volume where the file or folder is stored.

    We recommend that you set a maximum size for your cache and start clearing the cache whenever free space becomes low, or whenever you hit the maximum size. The fastest way to get the total size of your cache is:

    // A common file query by name will be "deep",
    // which means it will look at all subfolders
    var options = new Windows.Storage.Search.QueryOptions(Windows.Storage.Search.CommonFileQuery.orderByName, ["*"]);
    // This will get all files in your cache folder
    var fileQuery = cacheFolder.createFileQueryWithOptions(options);
    fileQuery.getFilesAsync().done(function(files) {
        var totalSize = 0;
        var promises = files.map(function(file) {
           return file.getBasicPropertiesAsync();
        });
        // Getting file size is async, so we wait for all promises to complete
        WinJS.Promise.join(promises).then(function(properties) {
            for (var i in properties) {
                totalSize += properties[i].size;
            }
        });
    });
    
    Hope this helps!
    • เสนอเป็นคำตอบโดย Marc Wautier - MSFTMicrosoft 28 มีนาคม 2555 0:19
    • ทำเครื่องหมายเป็นคำตอบโดย Vivian Ng 2 เมษายน 2555 3:36
    27 มีนาคม 2555 22:30

ตอบทั้งหมด

  • Hi Vivian,

    There is no limit on your local folders so you do not need to check this.

    How are you saving your files?  What is your concern?

    -Jeff


    Jeff Sanders (MSFT)

    • เสนอเป็นคำตอบโดย Jeff SandersMicrosoft, Moderator 13 มีนาคม 2555 17:43
    • ยกเลิกการนำเสนอเป็นคำตอบโดย Vivian Ng 19 มีนาคม 2555 3:51
    13 มีนาคม 2555 17:43
    ผู้ดูแล
  • Hi Jeft,

    I concern about the capacity of disk. My app will automatically download and save images to the cache. Then, the space for my app is increase day by day. So if the disk full or nearly full, I need to confirm user when they use my app, and suggest they delete old data. The behavior seems like windows notify you when you try to copy something large to disk which don't have enough space.

    Vivian

    14 มีนาคม 2555 3:05
  • Hi Vivian,

    Please do not fill the disk up or even get close this condition in your design.  Your application would be a very bad citizen if it uses all the disk space for itself.  I would recommend using the built in caching you get for resources by using the default mechanisms in HTML5.  If you absolutely must cache some files, limit the overall all size of the folder to a reasonable size and do not wait until you fill the entire disk with your data (I would uninstall your app if it started filling my disk unreasonably).  You can monitor a folder size so maybe that is a good compromise for you.  You can get the size from the folderproperties.  http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefolder.getbasicpropertiesasync.aspx

    -Jeff


    Jeff Sanders (MSFT)

    • เสนอเป็นคำตอบโดย Jeff SandersMicrosoft, Moderator 14 มีนาคม 2555 12:47
    • ยกเลิกการนำเสนอเป็นคำตอบโดย Vivian Ng 19 มีนาคม 2555 3:51
    14 มีนาคม 2555 12:47
    ผู้ดูแล
  • Hi Jeft,

    What happen if I limit the cache for my app is 2GB, but user just have 1GB in their disk. In this case, if we just monitor folder size, the app will be fail. Why don't we have the mechanisms that support we check the disk or notify us when we don't have enough space?

    15 มีนาคม 2555 2:56
  • Hi Jeft,

    Please think about the application which support user download video from youtube, etc. So you download and cache video to the disk. But if the disk is full or not enough for video data, I want to notify user or automatically delete old data of my application. That's why I need to check the disk size.

    Thanks,

    Vivian

    15 มีนาคม 2555 3:17
  • Now that makes sense!  I think we will simply throw an exception.  Let me see if I can find what that exception is for you.

    -Jeff


    Jeff Sanders (MSFT)

    15 มีนาคม 2555 12:30
    ผู้ดูแล
  • I tried to search many times, but no documents talk about the type of exception will be return. So if you found it, please let me know. Thank you!

    Vivian

    16 มีนาคม 2555 18:05
  • Hi Vivian,

    We also provide ways to determine the amount of available free space:

    System.FreeSpace can be retrieved with StorageItem.retrievePropertiesAsync and gives you the amount of free space for the volume where the file or folder is stored.

    We recommend that you set a maximum size for your cache and start clearing the cache whenever free space becomes low, or whenever you hit the maximum size. The fastest way to get the total size of your cache is:

    // A common file query by name will be "deep",
    // which means it will look at all subfolders
    var options = new Windows.Storage.Search.QueryOptions(Windows.Storage.Search.CommonFileQuery.orderByName, ["*"]);
    // This will get all files in your cache folder
    var fileQuery = cacheFolder.createFileQueryWithOptions(options);
    fileQuery.getFilesAsync().done(function(files) {
        var totalSize = 0;
        var promises = files.map(function(file) {
           return file.getBasicPropertiesAsync();
        });
        // Getting file size is async, so we wait for all promises to complete
        WinJS.Promise.join(promises).then(function(properties) {
            for (var i in properties) {
                totalSize += properties[i].size;
            }
        });
    });
    
    Hope this helps!
    • เสนอเป็นคำตอบโดย Marc Wautier - MSFTMicrosoft 28 มีนาคม 2555 0:19
    • ทำเครื่องหมายเป็นคำตอบโดย Vivian Ng 2 เมษายน 2555 3:36
    27 มีนาคม 2555 22:30
  • Hi Marc,

    I got it. Thank you so much!

    Vivian

    2 เมษายน 2555 3:36