Advertisement
Guest User

Untitled

a guest
Oct 8th, 2010
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Shapes;
  13. using System.Net;
  14. using System.IO;
  15.  
  16. namespace WpfApplication1
  17. {
  18.     /// <summary>
  19.     /// Interaction logic for ImageUpload.xaml
  20.     /// </summary>
  21.     public partial class ImageUpload : Window
  22.     {
  23.         public ImageUpload()
  24.         {
  25.             InitializeComponent();
  26.         }
  27.  
  28.         private void Button_Click(object sender, RoutedEventArgs e)
  29.         {
  30.             var postData = "";
  31.             var boundary = "---------FormBoundary-sadkd22dasd---------";
  32.             var template = "\r\n\r\n" + boundary + "\r\n" +
  33.                            "Content-Disposition: form-data; name=\"{0}\";" + "\r\n\r\n" +
  34.                            "{1}";
  35.             var req = HttpWebRequest.Create("http://www.imageshack.us/upload_api.php");
  36.             req.Method = "POST";
  37.             req.ContentType = "multipart/form-data; boundary=" + boundary;
  38.  
  39.             postData += string.Format(template, "key", "359EGLMNfb2a865f0a45adb4fd51e3f9f390739e");
  40.             postData += string.Format(template, "type", "base64");
  41.  
  42.             // get base64 data from image
  43.             byte[] bytes = File.ReadAllBytes(@"D:\tmp\WpfApplication1\WpfApplication1\Images\Icon128.gif");
  44.             string encoded = Convert.ToBase64String(bytes);
  45.             postData += string.Format("\r\n\r\n" + boundary + "\r\n" +
  46.                         "Content-Disposition: form-data; name=\"{0}\"; filename=\"Icon128.gif\" \r\n" +
  47.                         "Content-Type=image/gif \r\n\r\n" +
  48.                         "{1}", "fileupload", encoded);
  49.  
  50.             byte[] reqData = Encoding.UTF8.GetBytes(postData);
  51.             using (Stream dataStream = req.GetRequestStream())
  52.             {
  53.                 dataStream.Write(reqData, 0, reqData.Length);
  54.             }
  55.  
  56.             var res = (HttpWebResponse)req.GetResponse();
  57.             var resStream = res.GetResponseStream();
  58.             var reader = new StreamReader(resStream);
  59.             string resString = reader.ReadToEnd();
  60.             txt1.Text = resString;
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement