public FormFotoProducent() { InitializeComponent(); m_fotoserie = new Fotoserie(); m_klant = new Klant(); }
private void OnClickUpload(object sender, EventArgs e) { try { // maak een instantie van de klasse fotoserie zoals we die willen gaan // toevoegen aan de databse via de WebAPI. m_fotoserie.Naam = textBoxFotoSerieNaam.Text; m_fotoserie.KlantId = m_klant.Id; m_fotoserie.FotoproducentId = 0; m_fotoserie.Datum = DateTime.Now; m_fotoserie.Key = Utility.GenerateRandomString(20); // Maak JSON text van de fotoserie var serializer = new JavaScriptSerializer(); string JSON = serializer.Serialize(m_fotoserie); // stuur deze naar de webapi, we krijgen een JSON string // terug die de volledige Fotoserie wergeeft. // Dat is nodig om de Id te kunnen bepalen, we gaan er zo direct fotos // aan hangen. string url = "http://localhost:2372/api/Fotoserie/add"; var cli = new WebClient(); cli.Headers[HttpRequestHeader.ContentType] = "application/json"; string responseJSON = cli.UploadString(url, JSON); // response JSON vertalen naar een instantie van Klasse Fotoserie. m_fotoserie = serializer.Deserialize <Fotoserie>(responseJSON); MessageBox.Show("Fotoserie aangemaakt met ID " + m_fotoserie.Id); } catch (Exception ex) { MessageBox.Show(ex.Message); // ga niet verder; waarschijnlijk geen verbiniding met WebAPI return; } // Deel 2. Neem de lijst van fotos en upload ze. try { foreach (string fotoFileName in listBoxFotos.Items) { // haal de foto naar eeb byte array // Hoe groot is het bestand? FileInfo fotoFileInfo = new FileInfo(fotoFileName); long numBytes = fotoFileInfo.Length; FileStream fotoStream = new FileStream(fotoFileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fotoStream); // convert the file to a byte array byte[] fotoData = br.ReadBytes((int)numBytes); br.Close(); Uri webService = new Uri("http://localhost:2372/api/foto/" + m_fotoserie.Id + "/upload"); HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, webService); requestMessage.Headers.ExpectContinue = false; MultipartFormDataContent multiPartContent = new MultipartFormDataContent("----MyGreatBoundary"); ByteArrayContent byteArrayContent = new ByteArrayContent(fotoData); byteArrayContent.Headers.Add("Content-Type", "application/octet-stream"); multiPartContent.Add(byteArrayContent, Path.GetFileName(fotoFileName), fotoFileName); requestMessage.Content = multiPartContent; HttpClient httpClient = new HttpClient(); Task <HttpResponseMessage> httpRequest = httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None); HttpResponseMessage httpResponse = httpRequest.Result; HttpStatusCode statusCode = httpResponse.StatusCode; HttpContent responseContent = httpResponse.Content; if (responseContent != null) { Task <String> stringContentsTask = responseContent.ReadAsStringAsync(); String stringContents = stringContentsTask.Result; } fotoStream.Close(); fotoStream.Dispose(); } // geen exception, dus fotos zijn correct ge-upload. // maak daar een melding van. MessageBox.Show("Fotoserie '" + m_fotoserie.Naam + "' (" + listBoxFotos.Items.Count + " foto's) voor klant '" + m_klant.Naam + "' succesvol verzonden."); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void OnClickUpload(object sender, EventArgs e) { try { // maak een instantie van de klasse fotoserie zoals we die willen gaan // toevoegen aan de databse via de WebAPI. m_fotoserie.Naam = textBoxFotoSerieNaam.Text; m_fotoserie.KlantId = m_klant.Id; m_fotoserie.FotoproducentId = 0; m_fotoserie.Datum = DateTime.Now; m_fotoserie.Key = Utility.GenerateRandomString(20); // Maak JSON text van de fotoserie var serializer = new JavaScriptSerializer(); string JSON = serializer.Serialize(m_fotoserie); // stuur deze naar de webapi, we krijgen een JSON string // terug die de volledige Fotoserie wergeeft. // Dat is nodig om de Id te kunnen bepalen, we gaan er zo direct fotos // aan hangen. string url = "http://localhost:2372/api/Fotoserie/add"; var cli = new WebClient(); cli.Headers[HttpRequestHeader.ContentType] = "application/json"; string responseJSON = cli.UploadString(url, JSON); // response JSON vertalen naar een instantie van Klasse Fotoserie. m_fotoserie = serializer.Deserialize<Fotoserie>(responseJSON); MessageBox.Show("Fotoserie aangemaakt met ID " + m_fotoserie.Id); } catch (Exception ex) { MessageBox.Show(ex.Message); // ga niet verder; waarschijnlijk geen verbiniding met WebAPI return; } // Deel 2. Neem de lijst van fotos en upload ze. try { foreach (string fotoFileName in listBoxFotos.Items) { // haal de foto naar eeb byte array // Hoe groot is het bestand? FileInfo fotoFileInfo = new FileInfo(fotoFileName); long numBytes = fotoFileInfo.Length; FileStream fotoStream = new FileStream(fotoFileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fotoStream); // convert the file to a byte array byte[] fotoData = br.ReadBytes((int)numBytes); br.Close(); Uri webService = new Uri("http://localhost:2372/api/foto/" + m_fotoserie.Id + "/upload"); HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, webService); requestMessage.Headers.ExpectContinue = false; MultipartFormDataContent multiPartContent = new MultipartFormDataContent("----MyGreatBoundary"); ByteArrayContent byteArrayContent = new ByteArrayContent( fotoData ); byteArrayContent.Headers.Add("Content-Type", "application/octet-stream"); multiPartContent.Add(byteArrayContent, Path.GetFileName(fotoFileName), fotoFileName); requestMessage.Content = multiPartContent; HttpClient httpClient = new HttpClient(); Task<HttpResponseMessage> httpRequest = httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None); HttpResponseMessage httpResponse = httpRequest.Result; HttpStatusCode statusCode = httpResponse.StatusCode; HttpContent responseContent = httpResponse.Content; if (responseContent != null) { Task<String> stringContentsTask = responseContent.ReadAsStringAsync(); String stringContents = stringContentsTask.Result; } fotoStream.Close(); fotoStream.Dispose(); } // geen exception, dus fotos zijn correct ge-upload. // maak daar een melding van. MessageBox.Show("Fotoserie '" + m_fotoserie.Naam + "' (" + listBoxFotos.Items.Count + " foto's) voor klant '" + m_klant.Naam + "' succesvol verzonden."); } catch (Exception ex) { MessageBox.Show(ex.Message); } }