Compact Framework - Upload file via REST -
i looking best way transfer files compact framework server via rest. have web service created using .net web api. i've looked @ several questions , other sites dealt sending files, none of them seem work need.
i trying send media files wm 6 , 6.5 devices rest service. while of files less 300k, odd few may 2-10 or megabytes. have snippets use make work?
thanks!
i think minimum sending file:
using (var filestream = file.open(@"\file.txt", filemode.open, fileaccess.read, fileshare.read)) { httpwebrequest request = (httpwebrequest)httpwebrequest.create("http://www.destination.com/path"); request.method = "post"; // or put, depending on server expects request.contentlength = filestream.length; // see note below using (var requeststream = request.getrequeststream()) { int bytes; byte[] buffer = new byte[1024]; // reasonable buffer size while ((bytes = filestream.read(buffer, 0, buffer.length)) > 0) { requeststream.write(buffer, 0, bytes); } } try { using (httpwebresponse response = (httpwebresponse)request.getresponse()) { } } catch (webexception ex) { // failure } }
note: http needs way know when you're "done" sending data. there 3 ways achieve this:
- set
request.contentlength
used in example, because know size of file before sending anything - set
request.sendchunked
, send chunks of data including individual size - you could set
request.allowwritestreambuffering
write in-memory buffer, wouldn't recommend wasting memory on compact framework.
Comments
Post a Comment