예제 #1
0
        public async Task <Boolean> uploadOrginalImage(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();
                try
                {
                    // configure trmporery path
                    //string dir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
                    string dir     = Directory.GetCurrentDirectory();
                    string tmpPath = Path.Combine(dir, Global.TMPFolder);
                    tmpPath = Path.Combine(tmpPath, imageName + extension);
                    if (System.IO.File.Exists(tmpPath))
                    {
                        System.IO.File.Delete(tmpPath);
                    }
                    // resize image
                    ImageProcess imageP = new ImageProcess(1000, imagePath);
                    imageP.ScaleOrginalImage(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");

                        string fileName = imageName + extension;
                        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();
                    //delete tmp image
                    if (System.IO.File.Exists(tmpPath))
                    {
                        System.IO.File.Delete(tmpPath);
                    }
                }
                catch
                { return(false); }
            }
            return(false);
        }
예제 #2
0
        public async Task <Boolean> uploadImage(string imagePath, string imageName, int itemId)
        {
            if (imagePath != "")
            {
                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.TMPItemsFolder);

                    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(@"items/PostItemImage", form);
                        }
                        stream.Dispose();
                    }
                    // save image name in DB
                    Item item = new Item();
                    item.itemId = itemId;
                    item.image  = fileName;
                    await updateImage(item);

                    return(true);
                }
                catch
                { return(false); }
            }
            return(false);
        }
예제 #3
0
        public async Task <string> uploadImage(string imagePath, string imageName, int agentId)
        {
            if (imagePath != "")
            {
                //string imageName = agentId.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.TMPAgentsFolder);
                    tmpPath = Path.Combine(tmpPath, imageName + extension);
                    if (System.IO.File.Exists(tmpPath))
                    {
                        System.IO.File.Delete(tmpPath);
                    }
                    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(@"agent/PostUserImage", form);

                            //if (response.IsSuccessStatusCode)
                            //{
                            //    // save image name in DB
                            //    Agent agent = new Agent();
                            //    agent.agentId = agentId;
                            //    agent.image = fileName;
                            //    await updateImage(agent);
                            //    //await save();
                            //    return fileName;
                            //}
                        }
                        stream.Dispose();
                    }
                    // save image name in DB
                    Agent agent = new Agent();
                    agent.agentId = agentId;
                    agent.image   = fileName;
                    await updateImage(agent);

                    return(fileName);
                    //delete tmp image
                    //if (System.IO.File.Exists(tmpPath))
                    //{
                    //    System.IO.File.Delete(tmpPath);
                    //}
                }
                catch
                { return(""); }
            }
            return("");
        }