I need the ability to upload a file in my Xamarin app. How is this typically done for both iOS and Android and where do people host the files that have been uploaded for easy/quick retrieval?
Thanks! Tyler
You'll need to a WebService which is expecting the data. But this is the code I use to send bytes to the WebService.
async Task SendFileToServer(byte[] image, string id) { try { Uri webService = new Uri(URL + "imageUpload/" + id); using (var content = new MultipartFormDataContent("----MyGreatBoundary")) { using (var memoryStream = new MemoryStream(image)) { using (var stream = new StreamContent(memoryStream)) { content.Add(stream, "file", Guid.NewGuid().ToString() + ".jpg"); using (var message = await Client.PostAsync(webService, content)) { if (message.ReasonPhrase.ToLower() == "OK".ToLower()) { imagesToUpload.Remove(imagesToUpload.FirstOrDefault(f => f.imageBytes == image)); content.Dispose(); } } } } } } catch (Exception e) { } }
Answers
You'll need to a WebService which is expecting the data. But this is the code I use to send bytes to the WebService.