コード例 #1
0
        private void BAddImage_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Images|*.png;*.jpg;*.jpeg;*.gif";

            if (openFileDialog.ShowDialog() == true)
            {
                if (dgProducts.SelectedIndex != -1)
                {
                    Product p = dgProducts.SelectedItem as Product;

                    //sende image to server
                    WebClient webCli = new WebClient();
                    try {
                        byte[] responseArray = webCli.UploadFile(
                            Configs.Data.Global["imgSrvUrl"] + "product/" + p.Id.ToString() + "/picture?token=" + Configs.Data.Global["accessToken"],
                            "POST",
                            openFileDialog.FileName
                            );
                        String res = System.Text.Encoding.ASCII.GetString(responseArray);
                        if (res.Equals("none"))
                        {
                            MessageBox.Show(Application.Current.Resources["sErrorImageSending"] as String);
                        }
                        else
                        {
                            Picture newPic = new Picture();
                            newPic.PictureName = res;
                            newPic.UpdateAt    = DateTime.Now;
                            newPic.Product     = p;

                            newPic.Id = DbProduct.CreatePicture(newPic);

                            p.Pictures.Add(newPic);
                            lbPictures.Items.Refresh();
                        }
                    } catch {
                        MessageBox.Show(Application.Current.Resources["sErrorImageSending"] as String);
                    }
                }
            }
        }