예제 #1
0
        private void Button_UbahGambar_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.RestoreDirectory = true;
            openFileDialog.Title            = "Pilih Gambar";
            openFileDialog.DefaultExt       = "jpg";
            openFileDialog.Filter           = "Image (*.jpg;*.jpeg;)|*.jpg;*.jpeg;";
            openFileDialog.CheckFileExists  = true;
            openFileDialog.CheckPathExists  = true;
            //openFileDialog.ShowDialog();

            /*this.openFileDialog1.Multiselect = true;
             * foreach (String file in openFileDialog1.FileNames)
             * {
             *  MessageBox.Show(file);
             * }*/
            DialogResult dr = openFileDialog.ShowDialog();

            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                foreach (String file in openFileDialog.FileNames)
                {
                    if (openFileDialog.OpenFile().Length < 500 * 1024)
                    {
                        //Console.WriteLine(openFileDialog.FileName);
                        loadingDialog = new LoadingDialog();
                        loadingDialog.Show();
                        try
                        {
                            PictureBox imageControl = productPicture;

                            Image.GetThumbnailImageAbort myCallback =
                                new Image.GetThumbnailImageAbort(ThumbnailCallback);
                            Bitmap myBitmap    = new Bitmap(file);
                            Image  myThumbnail = myBitmap.GetThumbnailImage(300, 300,
                                                                            myCallback, IntPtr.Zero);
                            imageControl.Image = myThumbnail;
                            myBitmap.Dispose();
                            //

                            /*NameValueCollection nvc = new NameValueCollection
                             * {
                             *  { "id", "TTR" },
                             *  { "btn-submit-photo", "Upload" }
                             * };*/
                            string result = UploadImage.HttpUploadFile("https://kitku.id/produk/updatepic/" + filename,
                                                                       openFileDialog.FileName, null);
                            if (result.Contains("Success"))
                            {
                                MessageBox.Show("Gambar berhasil diupload!", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {
                                MessageBox.Show("Gambar gagal diupload!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            //Console.WriteLine("https://kitku.id/produk/updatepic/" + filename);
                            //
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Error: " + ex.Message);
                        }
                        loadingDialog.dismiss();
                    }
                    else
                    {
                        MessageBox.Show("Gambar hanya boleh berukuran 500kb atau kurang!", "Peringatan",
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
        private void ButtonSimpan_Click(object sender, EventArgs e)
        {
            buttonSimpan.Enabled = false;
            if (string.IsNullOrEmpty(boxIDSupplier.Text))
            {
                MessageBox.Show("Silakan dapatkan kode mitra terlebih dahulu dengan mengecek nama mitra", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (
                !string.IsNullOrEmpty(boxKategori.Text) ||
                !string.IsNullOrEmpty(boxKodeBarang.Text) ||
                !string.IsNullOrEmpty(boxNamaBarang.Text) ||
                !string.IsNullOrEmpty(boxSatuanInt.Text) ||
                !string.IsNullOrEmpty(boxHarga.Text) ||
                !string.IsNullOrEmpty(boxDeskripsi.Text) ||
                fileDialog != null
                )
            {
                // convert to json
                Datas datas = new Datas();
                datas.kategori    = boxKategori.Text;
                datas.id_barang   = boxKodeBarang.Text;
                datas.id_supplier = id_supplier;
                datas.nama        = boxNamaBarang.Text;
                datas.deskripsi   = boxDeskripsi.Text;
                datas.satuan      = boxSatuanInt.Text;
                datas.harga       = boxHarga.Text;
                var ser = datas.ToJson();
                Console.WriteLine(ser);

                bool dbSuccess = false, imageSuccess = false;

                // Specify requirement to POST
                //var stringData = await _httpClient.GetStringAsync();
                var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://kitku.id/produk/add");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method      = "POST";

                // write data
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    //string json = sr.ReadToEnd();
                    streamWriter.Write(ser);
                }

                // get response
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var res = Datas.FromJson(streamReader.ReadToEnd());
                    //Console.WriteLine(res.id_supplier);

                    // if data gotten
                    if (res.message != null)
                    {
                        dbSuccess        = true;
                        boxIDBarang.Text = res.id_barang;
                    }
                }

                string imageUploadMessage = UploadImage.HttpUploadFile("https://kitku.id/produk/updatepic/" + datas.nama.Replace(" ", ""), fileDialog.FileName, null);
                if (imageUploadMessage.Contains("Success"))
                {
                    imageSuccess = true;
                }

                if (dbSuccess && imageSuccess)
                {
                    MessageBox.Show("Produk berhasil ditambahkan!", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    if (!dbSuccess)
                    {
                        MessageBox.Show("Produk gagal ditambahkan!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    if (!imageSuccess)
                    {
                        MessageBox.Show("Gambar gagal diupload!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("Silakan lengkapi data dan gambar untuk menambah produk.", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            buttonSimpan.Enabled = true;
        }