2012-05-30 3 views
0

Я загружаю изображения из папки skydrive. Когда изображение загружается, мне нужно сохранить его в папке с именем 'pictures'Получить имя файла из потока (загрузка файла)

Но как я могу получить имя загруженного файла? Я пробовал следующий код, но фс возвращает нулевой

private void download() 
     { 
      if (ControlBackup_ID != null) 
      {    
       foreach (string it in contenidoSkyPic) 
       { 
        //MessageBox.Show (it); 
        infoTextBlock3.Text = "Downloading backup pictures..wait..."; 

        client.DownloadAsync(it + "/content"); 
        client.DownloadCompleted += new EventHandler<LiveDownloadCompletedEventArgs>(client_DownloadCompleted); 
       }    
      } 
      else 
       MessageBox.Show("Backup file of pictures doesn't exist!", "Error", MessageBoxButton.OK); 
     } 



void client_DownloadCompleted(object sender, LiveDownloadCompletedEventArgs e) 
    { 
     if (e.Error == null) 
     { 
      Stream stream = e.Result; //Need to write this into IS 
      FileStream fs = stream as FileStream; 

      if (fs != null) 
      { 
       try 
       { 
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
        { 
         using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("pictures\\" + fs.Name, FileMode.Create)) 
         { 
          stream.CopyTo(fileStream); 
          cantImatges_progreso_down += 1; 
         } 
        } 
       } 
       catch { } 

       if (cantImatges_progreso_down == contenidoSkyPic.Count()) 
       { 
        infoTextBlock3.Text = "Restore pictures completed!"; 
       } 
      } 

     } 
     else 
     { 
      // process error 
      MessageBox.Show("Restore pictures failed.", "Failure", MessageBoxButton.OK); 
     } 

     client.DownloadCompleted -= client_DownloadCompleted;    


    } 

ответ

1

Наконец, я нашел решение. Я видел, что я могу использовать "userstate" для передачи имени файла

Решение:

Когда я scann SkyDrive папки, теперь хранить идентификатор и имя файла:

List<KeyValuePair<string, string>> contenidoSkyPic ; 

void getFilesImatges_GetCompleted(object sender, LiveOperationCompletedEventArgs e) 
{ 
    List<object> data = (List<object>)e.Result["data"]; 

      contenidoSkyPic = new List<KeyValuePair<string, string>>(); 
      contenidoSkyPic.Clear(); 


      foreach (IDictionary<string, object> content in data) 
      { 
       contenidoSkyPic.Add(new KeyValuePair<string, string>((string)content["id"], (string)content["name"])); 
      } 


} 

Затем "Скачать" будет:

private void download() 
     { 
      if (ControlBackup_ID != null) 
      {    
       foreach (string it in contenidoSkyPic) 
       { 
        //MessageBox.Show (it); 
        infoTextBlock3.Text = "Downloading backup pictures..wait..."; 


LiveConnectClient client = new LiveConnectClient(session); 
client.DownloadCompleted += new EventHandler<LiveDownloadCompletedEventArgs>(client_DownloadImatgesCompleted); 
client.DownloadAsync(it.Key + "/content", it.Value); 

       }    
      } 
      else 
       MessageBox.Show("Backup file of pictures doesn't exist!", "Error", MessageBoxButton.OK); 
     } 

когда client_DownloadCompleted вызывается всякий раз, когда загрузка завершится, и я могу получить имя каждого файла:

void client_DownloadCompleted(object sender, LiveDownloadCompletedEventArgs e) 
    { 
     if (e.Error == null) 
     { 
      Stream stream = e.Result; //Need to write this into IS 
      string _namePicture = e.UserState.ToString(); 


       try 
       { 
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
        { 
         using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("pictures\\" + _namePicture , FileMode.Create)) 
         { 
          stream.CopyTo(fileStream); 
          cantImatges_progreso_down += 1; 
         } 
        } 
       } 
       catch { } 

       if (cantImatges_progreso_down == contenidoSkyPic.Count()) 
       { 
        infoTextBlock3.Text = "Restore pictures completed!"; 
       } 
      } 


     client.DownloadCompleted -= client_DownloadCompleted;    


    } 
0

Лучший способ implemebt это за счет использования фонового процесса

Пример реализации можно найти здесь:

http://msdn.microsoft.com/en-us/library/hh202959(v=vs.92).aspx

Но решение, которое лучше всего соответствует вашим потребностям, является примером браузера Skydrive:

http://code.msdn.microsoft.com/MetroSky-A-Complete-4250b80f или https://stackoverflow.com/questions/7346450/download-file-from-skydrive

Смежные вопросы