Wednesday 15 September 2010

c# - Using convertapi in Windows Store App -



c# - Using convertapi in Windows Store App -

im trying out convertapi in windows store app project, , want send .docx file , pdf file in return, im trying post im not sure how done, have far, not working.

private async task generatepdfcontract(string path) { seek { var info = new list < keyvaluepair < string, string >> { new keyvaluepair < string, string > ("api", "5"), new keyvaluepair < string, string > ("apikey", "419595049"), new keyvaluepair < string, string > ("file", "" + stream2), }; await postkeyvaluedata(data); } grab (exception e) { debug.writeline(e.message); }

}

private async task postkeyvaluedata(list < keyvaluepair < string, string >> values) { var httpclient = new httpclient(); var response = await httpclient.postasync("http://do.convertapi.com/word2pdf", new formurlencodedcontent(values)); var responsestring = await response.content.readasstringasync();

}

how should post send .docx file , .pdf file in return?

edit:

private async task generatepdfcontract(string path) { seek { using (var client = new system.net.http.httpclient()) { using (var multipartformdatacontent = new multipartformdatacontent()) { var values = new[] { new keyvaluepair<string, string>("apikey", "413595149") }; foreach (var keyvaluepair in values) { multipartformdatacontent.add(new stringcontent(keyvaluepair.value), string.format("\"{0}\"", keyvaluepair.key)); } storagefolder currentfolder = await applicationdata.current.localfolder.getfolderasync(constants.datadirectory); storagefile outputfile = await currentfolder.getfileasync("file.docx"); byte[] filebytes = await outputfile.tobytes(); //multipartformdatacontent.add(new bytearraycontent(fileio.readbufferasync(@"c:\test.docx")), '"' + "file" + '"', '"' + "test.docx" + '"'); multipartformdatacontent.add(new bytearraycontent(filebytes)); const string requesturi = "http://do.convertapi.com/word2pdf"; var response = await client.postasync(requesturi, multipartformdatacontent); if (response.issuccessstatuscode) { var responseheaders = response.headers; var paths = responseheaders.getvalues("outputfilename").first(); var path2 = path.combine(@"c:\", paths); storagefile samplefile = await applicationdata.current.localfolder.createfileasync(@"c:\users\thought\appdata\local\packages\xxxxx_apk0zz032bzya\localstate\data\"); await fileio.writebytesasync(samplefile, await response.content.readasbytearrayasync()); } else { debug.writeline("status code : {0}", response.statuscode); debug.writeline("status description : {0}", response.reasonphrase); } } } } grab (exception e) { debug.writeline(e.message); } }

@tomas tried adapt reply bit since there doesn't seem "file.readallbytes" on windows store apps, im getting response tho :\

you can't pass file stream string httpclient. utilize webclient.uploadfile method back upwards asynchronous uploads.

using (var client = new webclient()) { var filetoconvert = "c:\file-to-convert.docx"; var info = new namevaluecollection(); data.add("apikey", "413595149"); seek { client.querystring.add(data); var response = client.uploadfile("http://do.convertapi.com/word2pdf", filetoconvert); var responseheaders = client.responseheaders; var path = path.combine(@"c:\", responseheaders["outputfilename"]); file.writeallbytes(path, response); console.writeline("the conversion successful! word file {0} converted pdf , saved @ {1}", filetoconvert, path); } grab (webexception e) { console.writeline("exception message :" + e.message); if (e.status == webexceptionstatus.protocolerror) { console.writeline("status code : {0}", ((httpwebresponse)e.response).statuscode); console.writeline("status description : {0}", ((httpwebresponse)e.response).statusdescription); } } }

the illustration using httpclient()

using (var client = new system.net.http.httpclient()) { using (var multipartformdatacontent = new multipartformdatacontent()) { var values = new[] { new keyvaluepair<string, string>("apikey", "yourapikey") }; foreach (var keyvaluepair in values) { multipartformdatacontent.add(new stringcontent(keyvaluepair.value), string.format("\"{0}\"", keyvaluepair.key)); } multipartformdatacontent.add(new bytearraycontent(file.readallbytes(@"c:\test.docx")), '"' + "file" + '"', '"' + "test.docx" + '"'); const string requesturi = "http://do.convertapi.com/word2pdf"; var response = await client.postasync(requesturi, multipartformdatacontent); if (response.issuccessstatuscode) { var responseheaders = response.headers; var paths = responseheaders.getvalues("outputfilename").first(); var path = path.combine(@"c:\", paths); file.writeallbytes(path, await response.content.readasbytearrayasync()); } else { console.writeline("status code : {0}", response.statuscode); console.writeline("status description : {0}", response.reasonphrase); } } }

c# post windows-runtime windows-store-apps convertapi

No comments:

Post a Comment