Skip to content

Instantly share code, notes, and snippets.

@nodoid
Created October 27, 2015 21:43
Show Gist options
  • Save nodoid/f4b9a90510301eed05ae to your computer and use it in GitHub Desktop.
Save nodoid/f4b9a90510301eed05ae to your computer and use it in GitHub Desktop.
public static class SendData
{
readonly static string BASEURL = "https://www.someurl.com/api";
static string Url;
public static HttpWebRequest SetupRequest
{
get
{
var request = WebRequest.Create(Url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "multipart/form-data";
return request;
}
}
public static async Task<bool> HttpPost(MediaFile source, string token, string id, string folderid)
{
var res = await HttpPost(source, token, new FilesBase{ ACCid = id, FOLDERid = folderid });
return res;
}
public static async Task<bool> HttpPost(MediaFile source, string token, FilesBase files)
{
var Url = string.Format("{0}/upload/{1}/{2}/{3}", BASEURL, token, files.ACCid, files.FOLDERid);
byte[] fileArr = null;
DependencyService.Get<INetwork>().NetworkSpinner(true);
try
{
using (var streamReader = new StreamReader(source.Source))
{
using (var memstream = new MemoryStream())
{
streamReader.BaseStream.CopyTo(memstream);
fileArr = memstream.ToArray();
}
}
var imageStream = new ByteArrayContent(fileArr);
imageStream.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = Guid.NewGuid().ToString() + ".jpg"
};
var multi = new MultipartContent();
multi.Add(imageStream);
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(BASEURL);
var response = await client.PostAsync(new Uri(Url), multi);
DependencyService.Get<INetwork>().NetworkSpinner(false);
return response.IsSuccessStatusCode;
}
}
catch (Exception ex)
{
Debug.WriteLine("Exception sending data - {0}:{1}", ex.Message, ex.InnerException);
DependencyService.Get<INetwork>().NetworkSpinner(false);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment