Advertisement
Guest User

Untitled

a guest
Oct 8th, 2010
3,515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 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.Credentials = CredentialCache.DefaultCredentials;
  38.             req.ContentType = "multipart/form-data; boundary=" + boundary;
  39.  
  40.             postData += string.Format(template, "key", "359EGLMNfb2a865f0a45adb4fd51e3f9f390739e");
  41.             postData += string.Format(template, "type", "base64");
  42.  
  43.             // get base64 data from image
  44.             byte[] bytes = File.ReadAllBytes(@"D:\tmp\WpfApplication1\WpfApplication1\Images\Icon128.gif");
  45.             string encoded = Convert.ToBase64String(bytes);
  46.             postData += string.Format("\r\n\r\n" + boundary + "\r\n" +
  47.                         "Content-Disposition: form-data; name=\"{0}\"; filename=\"Icon128.gif\" \r\n" +
  48.                         "Content-Type=image/gif \r\n\r\n" +
  49.                         "{1}", "fileupload", encoded);
  50.             postData += "\r\n\r\n" + boundary + "--";
  51.  
  52.             byte[] reqData = Encoding.UTF8.GetBytes(postData);
  53.             req.ContentLength = reqData.Length;
  54.             using (Stream dataStream = req.GetRequestStream())
  55.             {
  56.                 dataStream.Write(reqData, 0, reqData.Length);
  57.             }
  58.  
  59.             var res = (HttpWebResponse)req.GetResponse();
  60.             var resStream = res.GetResponseStream();
  61.             var reader = new StreamReader(resStream);
  62.             string resString = reader.ReadToEnd();
  63.             txt1.Text = resString;
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement