更新条形码时如何保存图像?

时间:2018-09-27 11:49:45

标签: c#

当我保存图像时,它会被条形码名称保存。 但是,如果我修改条形码名称,则图像消失了,无法保存。 我在调试时注意到在更新条形码时 if(System.IO.File.Exists(imageFilePath))没有执行。但是在保存一个新文件时,它会执行。

该如何纠正?

更新代码

 public object Update(ItemEntity item)
    {
        var val = _itemRepository.Save(item);

        if (item.ImageFileName != string.Empty || item.ImageOriginalFileName != string.Empty)
        {
            string result = JsonConvert.SerializeObject(val);
            var definition = new { Success = false, EntryMode = string.Empty, BarCode = string.Empty, ItemCode = string.Empty };
            var info = JsonConvert.DeserializeAnonymousType(result, definition);
            if (info.Success)
            {
                if (!string.IsNullOrEmpty(item.ImageOriginalFileName))

                    this.SaveImage(item.ImageFileName, (item.ImageOriginalFileName == "BARCODE" ? info.ItemCode : item.ImageOriginalFileName == "BARCODEYes" ? item.Barcode : item.ImageOriginalFileName));
            }
        }
        if (item.DeleteImageFileName != string.Empty && item.ImageFileName == string.Empty) // Image file removed but no new image file is uploaded[Bug Id :34754 -No image is uploaded, when a user re-uploads an image after deleting a previous one.]
        {
            this.DeleteImage(item.DeleteImageFileName);
        }
        return val;
    }

保存代码

public void SaveImage(string fileName, string originalFileName)
        {
            string tempFileName = fileName;
            if (!string.IsNullOrEmpty(tempFileName))
            {
                // move file from temp location to actual loation
                var webAppPath = System.Web.Hosting.HostingEnvironment.MapPath("~");
                var splitPath = webAppPath.Split("\\".ToCharArray()).ToList();
                var upLevel = splitPath.Count - 1; // one level up
                splitPath = splitPath.Take(upLevel).ToList();
                var UIFolder = "WebUI";

                splitPath.AddRange(new string[] { UIFolder, "Temp", "ImageUpload", CurrentSession.UserId.Value.ToString(), "ItemImage" });

                var webUIPath = splitPath.Aggregate((i, j) => i + "\\" + j);

                var imageFilePath = Path.Combine(webUIPath.EnsureEndsWith('\\'), tempFileName);

                if (System.IO.File.Exists(imageFilePath))
                {
                    splitPath = webAppPath.Split("\\".ToCharArray()).ToList();
                    upLevel = splitPath.Count - 1; // one level up
                    splitPath = splitPath.Take(upLevel).ToList();

                    splitPath.AddRange(new string[] { "Shared", "ItemImage" });
                    webUIPath = splitPath.Aggregate((i, j) => i + "\\" + j);

                    // Check existence of folder, create if not found and Delete-Create if found
                    if (!Directory.Exists(webUIPath))
                    {
                        Logger.Debug("Create directory :" + webUIPath);
                        Directory.CreateDirectory(webUIPath);
                    }
                    originalFileName = originalFileName + ".jpg";
                    var imagDestPath = Path.Combine(webUIPath.EnsureEndsWith('\\'), originalFileName);

                    if (File.Exists(imagDestPath))
                    {
                        GC.Collect();
                        GC.WaitForPendingFinalizers();

                        Logger.Debug("Delete File :" + imagDestPath);
                        File.Delete(imagDestPath);
                    }

                    byte[] bytes = System.IO.File.ReadAllBytes(imageFilePath);
                    FileStream fs = new FileStream(imagDestPath, FileMode.Create, FileAccess.Write);
                    BinaryWriter bw = new BinaryWriter(fs);
                    bw.Write(bytes);
                    bw.Close();

                }
            }
        }

0 个答案:

没有答案