box api - upload file in box using box api 2.0 with ASp.net (4.0), c# -
hi want upload file in box using box api 2.0 asp.net (4.0), c#. getting 400 (bad request error).
following code.
`public bool upload(byte[] filebytes, string folderid, string filename) { uploadfilebytes = filebytes; uploadfilename = filename;
uploadsplitfile(folderid); return true; } public void uploadsplitfile(string folderid) { string filename = uploadfilename; byte[] buffer; using (memorystream resultstream = new memorystream()) { buffer = assemblefilesblock(); resultstream.write(buffer, 0, buffer.length); buffer = getformattedboundary(true); resultstream.write(buffer, 0, buffer.length); resultstream.flush(); buffer = resultstream.toarray(); } httpwebrequest myrequest = createrequest(buffer.length, folderid); using (stream newstream = myrequest.getrequeststream()) { newstream.write(buffer, 0, buffer.length); newstream.close(); } string response; using (httpwebresponse myhttpwebresponse = (httpwebresponse)myrequest.getresponse()) { using (stream responsestream = myhttpwebresponse.getresponsestream()) { textreader reader = new streamreader(responsestream); response = reader.readtoend(); } } } private byte[] assemblefile() { byte[] buffer; using (memorystream resultstream = new memorystream()) { buffer = encoding.ascii.getbytes(string.format("content-disposition: form-data; name=\"{0}\"; filename=\"{1}\"; parent_id=\"0\"{2}", guid.newguid(), path.getfilename(uploadfilename), environment.newline)); resultstream.write(buffer, 0, buffer.length); buffer = encoding.ascii.getbytes("content-type: application/octet-stream" + environment.newline + environment.newline); resultstream.write(buffer, 0, buffer.length); buffer = uploadfilebytes; resultstream.write(buffer, 0, buffer.length); buffer = encoding.ascii.getbytes(environment.newline); resultstream.write(buffer, 0, buffer.length); resultstream.flush(); buffer = resultstream.toarray(); } return buffer; } private byte[] assemblefilesblock() { byte[] buffer; using (memorystream resultstream = new memorystream()) { buffer = getformattedboundary(false); resultstream.write(buffer, 0, buffer.length); buffer = assemblefile(); resultstream.write(buffer, 0, buffer.length); resultstream.flush(); buffer = resultstream.toarray(); } return buffer; } private byte[] getformattedboundary(bool isendboundary) { string template = isendboundary ? "--{0}--{1}" : "--{0}{1}"; return encoding.ascii.getbytes(string.format(template, "aabbccx30", environment.newline)); } private httpwebrequest createrequest(int contentlength, string folderid) { httpwebrequest webrequest; webrequest = (httpwebrequest)webrequest.create("https://upload.box.com/api/2.0/files/content"); webrequest.method = "post"; webrequest.allowwritestreambuffering = true; webrequest.contenttype = string.concat("multipart/form-data;boundary=", "aabbccx30"); webrequest.headers.add("accept-encoding", "gzip,deflate"); webrequest.headers.add("accept-charset", "iso-8859-1"); webrequest.headers.add("authorization", "bearer access token"); webrequest.contentlength = contentlength; return webrequest; }`
try code.
i haven't used john hoerr's sdk, uses restsharp dll , upload api given in http://developers.box.com/docs/#files-upload-a-file.
the parameter folder_id id of folder want upload file.
public void upload_doc(string folder_id,string accesstoken) { var client = new restclient("https://upload.box.com/api/2.0"); var request = new restrequest("files/content", method.post); request.addparameter("parent_id", folder_id); request.addheader("authorization", "bearer " + accesstoken); string path = @"c:\users\xyz\desktop\homepage.png"; byte[] bytearray = system.io.file.readallbytes(path); request.addfile("filename", bytearray, "homepage.png"); var responses = client.execute(request); var content = responses.content; }
Comments
Post a Comment