SkyDrive REST API

d0jones
Messages postés
10
Date d'inscription
vendredi 29 décembre 2000
Statut
Membre
Dernière intervention
15 janvier 2013
- 2 mai 2012 à 11:10
Bonjour,
je veux uploader un fichier (de 5 à 10Mo) sur SkyDrive. Afin d'éviter le timeout, je souhaite envoyer plusieurs requêtes contenant chacune une partie du fichier.
string folderId = @"folder....!112";
var path = @"D:\...\file.pdf";
var uri = string.Format(@"{0}{1}/files?{2}", SkyDrive.Utilities.ApiServiceUri, folderId, SkyDrive.Utilities.AccessToken);
int count = 0;
int offset = 0;
// taille du block à envoyer
int bufferSize = 170000;
byte[] buffer = new byte[bufferSize];
using (var reader = new System.IO.FileStream(path, System.IO.FileMode.Open))
{
   while (offset < reader.Length)
   {
       offset += reader.Read(buffer, 0, bufferSize);
       count += bufferSize + 1;

       var request = (HttpWebRequest)WebRequest.Create(uri);
       request.Method = @"POST";
       request.Headers.Add(@"bits-packet-type", @"Fragment");
       request.Headers.Add(@"Content-Disposition", @"file; filename=""filename.pdf""");
       request.ContentType = @"multipart/form-data; boundary=AaB03x";

       using (var stream = request.GetRequestStream())
       {
           using (var writer = new System.IO.BinaryWriter(stream))
           {
               try
               {
                   writer.Write(@"--AaB03x\r\n");
                   writer.Write(@"Content-Disposition: form-data; name=""file""; filename=""filename.pdf""\r\n");
                   writer.Write(@"Content-Type: application/octet-stream\r\n\r\n");
                   writer.Write(buffer);
                   writer.Write(@"\r\n\r\n--AaB03x--");
                }
                catch (System.IO.EndOfStreamException excep)
                {
                   throw new ApplicationException(excep.Message);
                }
           }
       }

       using (var stream = request.GetResponse().GetResponseStream())
       {
           using (var responseReader = new System.IO.StreamReader(stream))
           {
               var response = responseReader.ReadToEnd();
                            Console.WriteLine(response);
           }
       }
   }
}

je prends une http 400 :
{
   "error": {
      "code": "request_body_invalid", 
      "message": "The provided request entity body is not valid."
   }
}


Si cela vous inspire...
Merci,
BriceR