Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using System.Net;
- using System.IO;
- namespace WpfApplication1
- {
- /// <summary>
- /// Interaction logic for ImageUpload.xaml
- /// </summary>
- public partial class ImageUpload : Window
- {
- public ImageUpload()
- {
- InitializeComponent();
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- var postData = "";
- var boundary = "---------FormBoundary-sadkd22dasd---------";
- var template = "\r\n\r\n" + boundary + "\r\n" +
- "Content-Disposition: form-data; name=\"{0}\";" + "\r\n\r\n" +
- "{1}";
- var req = HttpWebRequest.Create("http://www.imageshack.us/upload_api.php");
- req.Method = "POST";
- req.ContentType = "multipart/form-data; boundary=" + boundary;
- postData += string.Format(template, "key", "359EGLMNfb2a865f0a45adb4fd51e3f9f390739e");
- postData += string.Format(template, "type", "base64");
- // get base64 data from image
- byte[] bytes = File.ReadAllBytes(@"D:\tmp\WpfApplication1\WpfApplication1\Images\Icon128.gif");
- string encoded = Convert.ToBase64String(bytes);
- postData += string.Format("\r\n\r\n" + boundary + "\r\n" +
- "Content-Disposition: form-data; name=\"{0}\"; filename=\"Icon128.gif\" \r\n" +
- "Content-Type=image/gif \r\n\r\n" +
- "{1}", "fileupload", encoded);
- byte[] reqData = Encoding.UTF8.GetBytes(postData);
- using (Stream dataStream = req.GetRequestStream())
- {
- dataStream.Write(reqData, 0, reqData.Length);
- }
- var res = (HttpWebResponse)req.GetResponse();
- var resStream = res.GetResponseStream();
- var reader = new StreamReader(resStream);
- string resString = reader.ReadToEnd();
- txt1.Text = resString;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement