c# - Google Drive API File Upload Error: "Missing end boundary in multipart body." -


i'm attempting multipart upload google via web request, , i've followed google's instructions on how construct valid multipart file upload request can send metadata , actual file data @ same time, keep getting "missing end boundary in multipart body." error when try , upload file , out of ideas why. doing wrong?

also, i'm not using drive sdk did not suit needs.

here's code:

 public bool writefiledata(stream data, dsfile file, dsuser user)     {                  var parent = new parent();         var folders = getuserfolders(user, false);         dsfolder parentfolder = folders.where(f => f.fullpath == file.virtualpath).firstordefault();         parent.id = parentfolder.depositoryfolderid;          var addfilerequest = new addfilerequest();         addfilerequest.parents.add(parent);         addfilerequest.title = (file.filename.tolower().contains(".ext") == false) ? file.filename + ".ext" : file.filename;         addfilerequest.modifieddate = serviceutil.toiso8601(datetime.now);         addfilerequest.mimetype = "application/octet-stream";         addfilerequest.writerscanshare = false;         addfilerequest.description = file.description;         addfilerequest.labels = new filelabels();          byte[] bindata = new byte[data.length];         data.read(bindata, 0, (int)data.length);          string metadata = microsoft.http.httpcontentextensions.createjsondatacontract<addfilerequest>(addfilerequest).readasstring();         string bindata64 = convert.tobase64string(bindata);          string contentstring = "--123abc content-type: application/json; charset=utf-8 " + metadata;         contentstring += "--123abc content-type: application/octet-stream " + bindata64;         contentstring += " --123abc--";          httpresponsemessage response;          try         {             httpclient client = new httpclient();             addauthheader(client, credential.accesstoken);             client.defaultheaders.contenttype = "multipart/related; boundary=\"123abc\"";             client.defaultheaders.contentlength = httpcontent.create(contentstring).readasbytearray().length;             response = client.post("https://www.googleapis.com/upload/drive/v2/files?uploadtype=multipart", httpcontent.create(contentstring));              string responsetext = response.content.readasstring();             return false;         }         catch (exception ex)         {             return false;         }     } 

edit: here's definition addfilerequest:

    [datacontract]     public class addfilerequest     {         [datamember(name="title")]         public string title { get; set; }         [datamember(name = "labels")]         public filelabels labels { get; set; }         [datamember(name = "mimetype")]         public string mimetype { get; set; }         [datamember(name = "modifieddate")]         public string modifieddate { get; set; }         [datamember(name = "parents")]         public list<parent> parents { get; set; }         [datamember(name = "description")]         public string description { get; set; }         [datamember(name="writerscanshare")]         public bool writerscanshare { get; set; }    } 

found solution. decided poke around object browser , noticed system.net.http namespace has "multipartformdatacontent" class did trick. didn't see before because, reason, there 2 different sets of identical (but incompatible) http namespaces: microsoft.http , system.net.http. here's updated code works:

public bool writefiledata(stream data, dsfile file, dsuser user)         {                   var parent = new parent();             var folders = getuserfolders(user, false);             dsfolder parentfolder = folders.where(f => f.fullpath == file.virtualpath).firstordefault();             parent.id = parentfolder.depositoryfolderid;              var addfilerequest = new addfilerequest();             addfilerequest.parents.add(parent);             addfilerequest.title = (file.filename.tolower().contains(".ext") == false) ? file.filename + ".ext" : file.filename;             addfilerequest.modifieddate = serviceutil.toiso8601(datetime.now);             addfilerequest.mimetype = "application/octet-stream";             addfilerequest.writerscanshare = false;             addfilerequest.description = file.description;             addfilerequest.labels = new filelabels();              string metadata = microsoft.http.httpcontentextensions.createjsondatacontract<addfilerequest>(addfilerequest).readasstring();              var content = new system.net.http.multipartformdatacontent("abc123");             content.add(new system.net.http.stringcontent(metadata, system.text.encoding.utf8, "application/json"));             content.add(new system.net.http.streamcontent(data));              try             {                             var client = new system.net.http.httpclient();                 client.defaultrequestheaders.authorization = new system.net.http.headers.authenticationheadervalue("bearer", credential.accesstoken);                 var response = client.postasync("https://www.googleapis.com/upload/drive/v2/files?uploadtype=multipart", content).result;                  string responsetext = response.content.readasstringasync().result;                 return false;             }             catch (exception ex)             {                 return false;             }         } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -