locked
create file from encoded base64 string winrt windows8

    Question

  • I want to decode a string and create file using Filestream in winrt, i know Filestream is not available in winrt model. I tried many options like DataReader,StreamReader but problem is i am getting file content and filename in single base64 encoded text, i can't predefine filename.
    file encoding is like : http://snipplr.com/view/6034/

    if i am using .net than i can easily do like this:

       byte[] data = Convert.FromBase64String(base64Encode);
           
       
           using (var fileStream = new FileStream(@"C:\NDv\NC\GloWin.zip", FileMode.Create))
           {
               fileStream.Write(data, 0, data.Length);
    
           }

    but how can i achieve this in winrt (windows component runtime) .....

    if any body have idea please reply with source code.

    thanks

    Friday, March 7, 2014 3:41 AM

Answers

  • The code we gave you earlier worked fine. Here's the working code for your reference:

    string base64Encode = @"<removed encoding>";
    byte[] data = Convert.FromBase64String(base64Encode);
    StorageFile ZipFile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("GloWin.zip");
    await FileIO.WriteBytesAsync(ZipFile, data);

    What happened is that the file was created inside the App's Localfolder.  That's located in the App's install location.  For my app, this was the path:

    C:\Users\msmall\AppData\Local\Packages\cb5d0e03-be18-4576-be1e-39e8297ca04f_mw36wsv5647dj\LocalState

    Here's a screenshot:

    Now, to answer a question you're going to have:  No, you can't save this to "c:\NC\GloWin.zip" due to the sandboxed environment of WinRT apps.

    Next, your question about having the name of the app inside the stream doesn't make any sense to us.  The fileGloWin.zip contains a file called GLOBAL.dmp.  Does that help?

    If you ever need to figure out the location of a written file, you can use Process Monitor to figure out where the file was written:


           


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


    Wednesday, March 19, 2014 5:43 PM
    Moderator
  • Don't open the zip file in Notepad, unzip it and then open the file global.dmp.

    I just did this and it looks normal:


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, March 19, 2014 6:20 PM
    Moderator

All replies

  • I'm really not clear on what you're asking, but it sounds like you are getting a Base64 encoded stream which contains both filename and file content. I would create a temporary file (StorageFile) that contains all of the data from the stream.  Extract the filename from the file, then save the file as the name.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, March 7, 2014 1:35 PM
    Moderator
  • actually base64 string is retuning file :

    HI please copy encoded string from here : https://onedrive.live.com/redir?resid=2199CC4CC50F4F5D!429&authkey=!AEe7szt9MzbJ750&ithint=file%2c.txt

    and run this program

    string base64Encode=@""//paste string here
    byte[] data = Convert.FromBase64String(base64Encode);
           
       
           using (var fileStream = new FileStream(@"C:\NC\GloWin.zip", FileMode.Create))
           {
               fileStream.Write(data, 0, data.Length);
    
           }
    you can understand what i am trying to explain. i want to exact the same thing in WinRT....

    Saturday, March 8, 2014 8:58 AM
  • hi please any one tell is this possible in winrt??
    Sunday, March 9, 2014 6:40 PM
  • I think this is what you want:

    byte[] data = Convert.FromBase64String(Base64Text);
    StorageFile ZipFile = await Windows.Storage.KnownFolders.DocumentsLibrary.CreateFileAsync("GloWin.zip");
    await FileIO.WriteBytesAsync(ZipFile, data);
    You can't arbitrarily put files anywhere.  They have to be in locations selected by the FilePicker, or in a KnownFolder.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, March 10, 2014 6:42 PM
    Moderator
  • hi, this code just creating blank zip file, not creating any file...

     public static async Task<string> GetInstancex()
            {
                try
                {
                    string nname = @"bmFyZW5kcmEuZHV0dGE=";
     byte[] data = Convert.FromBase64String(nname);
                    StorageFile ZipFile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("GloWin.zip");
                   await FileIO.WriteBytesAsync(ZipFile, data); //file not creating
                    return "Completed";
                }
                catch (Exception ex)
                {
                    return getMessage = ex.Message.ToString();
                    
                }
    
            }

    and i am calling this method like this:

     public static void GetInstancexx()
            {
                Task t = ClassLibrarynkd.Class1.GetInstancex();
            }
    can you please check what i am missing..


    • Edited by nkd108 Wednesday, March 12, 2014 7:09 AM
    Wednesday, March 12, 2014 7:07 AM
  • Don't forget to close the file after writing.  The file contents are probably being cached in memory.

    Darin R.

    Wednesday, March 12, 2014 10:49 AM
  • hi please tell me how to close the file writing according to my code.. i don't have extensive knowledge of asyns.. please help
    Wednesday, March 12, 2014 12:21 PM
  • Sorry - I must have been reading something else while writing my post. I was wrong.  What you're doing is fine - the StorageFile will get disposed of and cleaned up leaving your try block.

    What happens when you do this:

    public static async void GetInstancexx()
            {
                string s = await ClassLibrarynkd.Class1.GetInstancex();
            }
    ... instead of your code sample above?

    What does s equal after the ClassLibrarynkd.Class1.GetInstancex() call? 


    Darin R.

    Wednesday, March 12, 2014 5:00 PM
  • it is creating a zip like a file like .txt and writing the data .

    but zip should be a compressed folder and inside zip a file should be created as per base64 code.

    please run my posted code : it is creating a zip file and inside zip creating a "global.dmp" file in .net , just like this i want to do samething in winrt, please run my code so that you can understand my problem.

    HI please copy encoded string from here : https://onedrive.live.com/redir?resid=2199CC4CC50F4F5D!429&authkey=!AEe7szt9MzbJ750&ithint=file%2c.txt
    
    and run this program
    
    string base64Encode=@""//paste string here
    byte[] data = Convert.FromBase64String(base64Encode);
           
       
           using (var fileStream = new FileStream(@"C:\NC\GloWin.zip", FileMode.Create))
           {
               fileStream.Write(data, 0, data.Length);
    
           }
    

    • Edited by nkd108 Thursday, March 13, 2014 8:43 AM
    Thursday, March 13, 2014 7:09 AM
  • please reply i really want any alternative.
    Sunday, March 16, 2014 9:35 PM
  • As Matt said, the obvious problem is this line:

    var fileStream = new FileStream(@"C:\NC\GloWin.zip", FileMode.Create)

    Your app doesn't have access to c:\NC and so can't create a file there. You'll need to save the file to your app data or through a StorageFile. See my blog entry      Skip the path: stick to the StorageFile

    Sunday, March 16, 2014 11:34 PM
    Owner
  • my problem is, i want to use "filestream"  in winRT, i posted my .net working code above,  i am geeting file(a complete file, not only content) in base64, my .net code using filestream create a zip file and after that it is automatically creating a file(i am not defining any file type like txt,xml). 

    i just want to do the same activity in winRT.

     and i am using this path for creating files "Windows.Storage.Application.Current.LocalFolder.Path"


    • Edited by nkd108 Tuesday, March 18, 2014 6:29 AM
    Tuesday, March 18, 2014 6:27 AM
  • See Quickstart: Reading and writing files for guidance on using the Windows Runtime file api.

    --Rob

    Wednesday, March 19, 2014 12:07 AM
    Owner
  • i already tried it, first i don't know what type of file(a complete file in encrypted form) is returning my base64 code so i can't predefine any file type. mostly i am getting .dmp file second it(filestream) is automatically creating file.

    • Edited by nkd108 Wednesday, March 19, 2014 9:11 AM
    Wednesday, March 19, 2014 8:09 AM
  • Hi nkd,

    You keep pointing back to your original post saying it does what you want.... However it does not do what you describe.  It does not extract the file name from the stream.

    Also, there is no standard for adding or extracting the filename in the stream or base 64 encoded string.

    Here is an action plan for you to follow:

    1.  If you do not complete this step first, no one can help you so...  Determine exactly what the stream coming to you does and contains.  There is NO standard for embedding a filename in a stream so you need to figure that out from the particular source you are reading from.  Once you know that information use that information to do the reset of the steps.

    2.  Extract and save the name of the file from the stream.

    3.  Extract the rest of the data (file contents from the stream).

    4.  Given the information from the previous steps, use the suggestions everyone has given you so far.

    Jeff


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Wednesday, March 19, 2014 1:26 PM
    Moderator
  • https://onedrive.live.com/redir?resid=2199CC4CC50F4F5D!432&authkey=!ABYp6NuFot0d5G4&ithint=file%2c.rar

    please check it , please run it...please

    Wednesday, March 19, 2014 2:46 PM
  • Please share your minimal sample as a zip file so it can be opened without third party software.
    Wednesday, March 19, 2014 2:56 PM
    Owner
  • https://onedrive.live.com/redir?resid=2199CC4CC50F4F5D!433&authkey=!AAGac4ZTdpAwk1A&ithint=file%2c.zip


    create NC folder in c drive than run i want to do exact same thing in winrt.
    • Edited by nkd108 Wednesday, March 19, 2014 4:27 PM
    Wednesday, March 19, 2014 4:25 PM
  • The code we gave you earlier worked fine. Here's the working code for your reference:

    string base64Encode = @"<removed encoding>";
    byte[] data = Convert.FromBase64String(base64Encode);
    StorageFile ZipFile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("GloWin.zip");
    await FileIO.WriteBytesAsync(ZipFile, data);

    What happened is that the file was created inside the App's Localfolder.  That's located in the App's install location.  For my app, this was the path:

    C:\Users\msmall\AppData\Local\Packages\cb5d0e03-be18-4576-be1e-39e8297ca04f_mw36wsv5647dj\LocalState

    Here's a screenshot:

    Now, to answer a question you're going to have:  No, you can't save this to "c:\NC\GloWin.zip" due to the sandboxed environment of WinRT apps.

    Next, your question about having the name of the app inside the stream doesn't make any sense to us.  The fileGloWin.zip contains a file called GLOBAL.dmp.  Does that help?

    If you ever need to figure out the location of a written file, you can use Process Monitor to figure out where the file was written:


           


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


    Wednesday, March 19, 2014 5:43 PM
    Moderator
  • file location is not an issue, my issue is how can i create file like filestream , you can see it is creating a file global.dmp which is i am getting from base64 if you open this(global.dmp) in notepad it contains some sql queries.

    i tried your posted code it is creating a zipfile as a file and write, if you open this zipfile in notepad than you found some unusual character . because base64 is returning a complete file global.dmp not just contents....


    • Edited by nkd108 Wednesday, March 19, 2014 6:11 PM
    Wednesday, March 19, 2014 5:56 PM
  • Don't open the zip file in Notepad, unzip it and then open the file global.dmp.

    I just did this and it looks normal:


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, March 19, 2014 6:20 PM
    Moderator
  • Matt is correct.

    Understand that the ZIP format DOES contain characters in it that are related to the ZIP format encoding itself.

    Jeff


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Wednesday, March 19, 2014 6:21 PM
    Moderator
  • thanks @Matt Small, now it is working. previously i don't know why it was creating corrupt zip file but now everything is working fine...

    if i am not bothering you can you please tell me how can i bulk run these sqlquery in sqlite async way i am following : http://code.msdn.microsoft.com/windowsapps/Sqlite-For-Windows-8-Metro-2ec7a882 this way to create app

    Thursday, March 20, 2014 7:47 AM
  • Please posts new questions as a new question on the forum.  Thanks!


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Thursday, March 20, 2014 12:22 PM
    Moderator