예제 #1
0
        // update image field in DB
        public async Task <int> updateImage(DocImage docImage)
        {
            Dictionary <string, string> parameters = new Dictionary <string, string>();
            string method = "DocImage/UpdateImage";

            var myContent = JsonConvert.SerializeObject(docImage);

            parameters.Add("Object", myContent);
            return(await APIResult.post(method, parameters));

            //string message = "";

            //// ... Use HttpClient.
            //ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

            //string myContent = JsonConvert.SerializeObject(docImage);

            //using (var client = new HttpClient())
            //{
            //    ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            //    client.BaseAddress = new Uri(Global.APIUri);
            //    client.DefaultRequestHeaders.Clear();
            //    client.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
            //    client.DefaultRequestHeaders.Add("Keep-Alive", "3600");

            //    HttpRequestMessage request = new HttpRequestMessage();

            //    // encoding parameter to get special characters
            //    myContent = HttpUtility.UrlEncode(myContent);

            //    request.RequestUri = new Uri(Global.APIUri + "DocImage/UpdateImage?docImageObject=" + myContent);
            //    request.Headers.Add("APIKey", Global.APIKey);
            //    request.Method = HttpMethod.Post;
            //    //set content type
            //    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            //    var response = await client.SendAsync(request);

            //    if (response.IsSuccessStatusCode)
            //    {
            //        message = await response.Content.ReadAsStringAsync();
            //        message = JsonConvert.DeserializeObject<string>(message);
            //    }
            //    return message;
            //}
        }
예제 #2
0
        public async Task <Boolean> uploadImage(string imagePath, string tableName, int docImageId)
        {
            if (imagePath != "")
            {
                string imageName = Md5Encription.MD5Hash(tableName + docImageId.ToString());
                MultipartFormDataContent form = new MultipartFormDataContent();
                // get file extension
                var    ext       = imagePath.Substring(imagePath.LastIndexOf('.'));
                var    extension = ext.ToLower();
                string fileName  = imageName + extension;
                try
                {
                    // configure trmporery path
                    string dir     = Directory.GetCurrentDirectory();
                    string tmpPath = Path.Combine(dir, Global.TMPFolder);

                    string[] files = System.IO.Directory.GetFiles(tmpPath, imageName + ".*");
                    foreach (string f in files)
                    {
                        System.IO.File.Delete(f);
                    }

                    tmpPath = Path.Combine(tmpPath, imageName + extension);
                    if (imagePath != tmpPath) // edit mode
                    {
                        // resize image
                        ImageProcess imageP = new ImageProcess(150, imagePath);
                        imageP.ScaleImage(tmpPath);

                        // read image file
                        var stream = new FileStream(tmpPath, FileMode.Open, FileAccess.Read);

                        // create http client request
                        using (var client = new HttpClient())
                        {
                            client.BaseAddress = new Uri(Global.APIUri);
                            client.Timeout     = System.TimeSpan.FromSeconds(3600);
                            string      boundary = string.Format("----WebKitFormBoundary{0}", DateTime.Now.Ticks.ToString("x"));
                            HttpContent content  = new StreamContent(stream);
                            content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
                            content.Headers.Add("client", "true");

                            content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                            {
                                Name     = imageName,
                                FileName = fileName
                            };
                            form.Add(content, "fileToUpload");

                            var response = await client.PostAsync(@"DocImage/PostImage", form);

                            //if (response.IsSuccessStatusCode)
                            //{
                            //    // save image name in DB
                            //    DocImage docImage = new DocImage();
                            //    docImage.id = docImageId;
                            //    docImage.image = fileName;
                            //    await updateImage(docImage);
                            //    //await save();
                            //    return true;
                            //}
                        }
                        stream.Dispose();
                    }
                    // save image name in DB
                    DocImage docImage = new DocImage();
                    docImage.id    = docImageId;
                    docImage.image = fileName;
                    await updateImage(docImage);

                    return(true);
                }
                catch
                { return(false); }
            }
            return(false);
        }