Write file specific folder inside localFolder

2

I'm trying to write an image file to the localFolder storage. I want to write it specifically to the images/heroes/ folder inside the localFolder. Is there some way to do this with my existing code?

return WinJS.xhr({ url: fileToDownloadURL, responseType: 'blob' }).then(
   function (response) {
      var input = response.response.msDetachStream();
      var filename = 'ms-appx:///local/images/heroes/image_name.png';

      Windows.Storage.ApplicationData.current.localFolder.createFileAsync(filename,
      Windows.Storage.CreationCollisionOption.replaceExisting).then(
         function (file) {
            return file.openAsync(Windows.Storage.FileAccessMode.readWrite).then(
               function (output) {
                  return Windows.Storage.Streams.RandomAccessStream.copyAsync(input, output).then(
                     function () {
                        output.flushAsync().then(function () {
                           input.close();
                           output.close();
                     });
                });
          });
     }
   }
)};

I get this error message:

0x8007007b - JavaScript runtime error: The filename, directory name, or volume label syntax is incorrect.

WinRT information: The specified name (ms-appx:///local/images/heroes/image_name.png) contains one or more invalid characters.

windows-8
visual-studio-2012
winjs
asked on Stack Overflow Jan 27, 2013 by devinwl • edited Jan 27, 2013 by devinwl

1 Answer

5

I figured it out. For anyone wondering, you can call

getFolderAsync('SomeFolderName')

from the

Windows.Storage.ApplicationData.current.localFolder

instance. getFolderAsync() is like any promise function and will return with an error if the folder does not exist. From there you can run

createFolderAsync('SomeFolderName')

to create the folder, and then continue what you were doing. In my case it was better to create a function like writeFileAsync() and then call it depending on whether or not the folder exists first.

Keep in mind getFolderAsync() can only get one folder at a time, so something like

getFolderAsync('along/list/of/folders')

won't work. You'll have to chain together the getFolderAsync() calls together, I believe (correct me if I'm wrong!).

answered on Stack Overflow Jan 27, 2013 by devinwl

User contributions licensed under CC BY-SA 3.0