How to get the downloaded file programmatically in Windows Phone 7?

I upload an epub file online. To do this, I first created the directory using Directory.CreateDirectory , then uploaded the file using the following code.

 WebClient webClient = new WebClient(); webClient.DownloadStringAsync(new Uri(downloadedURL), directoryName); webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Completed); 

Is this the right way to upload files? What is the code to view the downloaded file and display it in the grid?

+4
source share
2 answers

1) Do not use Directory.CreateDirectory on Windows Phone. Instead, since you are working with isolated storage, you need to use:

 var file = IsolatedStorageFile.GetUserStoreForApplication(); file.CreateDirectory("myDirectory"); 

2) Downloading files can be done through WebClient as follows:

 WebClient client = new WebClient(); client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted); client.OpenReadAsync(new Uri("your_URL")); void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { var file = IsolatedStorageFile.GetUserStoreForApplication(); using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("file.epub", System.IO.FileMode.Create, file)) { byte[] buffer = new byte[1024]; while (e.Result.Read(buffer, 0, buffer.Length) > 0) { stream.Write(buffer, 0, buffer.Length); } } } 

Creating a directory in this case is optional. If you need to save the file in a subfolder structure, you can also set the file path to something like /Folder/NewFolder/file.epub .

3) To list files in isolated storage, you can use:

 var file = IsolatedStorageFile.GetUserStoreForApplication(); file.GetFileNames(); 

This is if the files are in the root directory of IsoStore. If they are inside the directory, you will need to install the search template and pass it to GetFileNames - including the folder name and file type. For each individual file, you can use this template:

 DIRECTORY_NAME\*.* 
+8
source

No file. The argument to the DownloadStringCompleted event contains a Result element containing a string that is the result of your HTTP request. In the event handler you can refer to this as e.Result

I am not familiar with the epub file format, but if they are not strictly text files, your code may not work properly.

Instead, you should use webclient.OpenReadAsync and the corresponding event, which is similar to DownloadStringAsync in the methodology, except that e.Result is a stream that you can use to read binary data.

+1
source

All Articles