简体   繁体   English

如何将文件保存在临时文件夹中?

[英]How can I save files inside a temporary folder?

I am obtaining the directory of the temp or tmp folder, and creating a folder called "TemporaryImages" from it, which as the name says, I am only going to use it to save images with low resolution temporarily.我正在获取temptmp文件夹的目录,并从中创建一个名为“TemporaryImages”的文件夹,顾名思义,我只是暂时使用它来保存低分辨率的图像。

The problem is that "TemporaryImages" is being created successfully, but the images are not being saved within it, and I don't understand where I'm screwing up.问题是“TemporaryImages”已成功创建,但图像没有保存在其中,我不明白我在哪里搞砸了。

I'm using C# ASP.NET, on a Linux server.我在 Linux 服务器上使用 C# ASP.NET。

This code receives a list of images fetched from a database.此代码接收从数据库中获取的图像列表。 With "TemporalFolder()" I create or get the directory of the temporary folder, and with the function "SaveImage()" I am sending it the name of the image, the directory of the folder, and the new size of the image.使用“TemporalFolder()”我创建或获取临时文件夹的目录,并使用 function“SaveImage()”向它发送图像的名称、文件夹的目录和图像的新大小。

private void ChangeImageSize(List<OrderViewModel> orderViewModels)
        {
            const int thumbnailWidth = 500;
            string tempFolder;

            try
            {
                tempFolder = TemporaryFolder();

                foreach (var item in orderViewModels)
                {
                    var imageResult = Image.Load("wwwroot/" + item.getImages());

                    this.SaveImage(imageResult, tempFolder, thumbnailWidth);
                }
            }
            catch (Exception ex)
            {
                ViewBag.error = ex.Message.ToString();
                throw new Exception(ex.Message);
            }
        }

With this function I seek to save the image inside the temporary folder:有了这个 function 我试图将图像保存在临时文件夹中:

  private void SaveImage(Image image, string name, int resizeWidth)
{
    var width = image.Width;
    var height = image.Height;

    if (width > resizeWidth)
    {
        height = (int)((double)resizeWidth / width * height);
        width = resizeWidth;
    }

    picture
        .Mutate(i => i.Resize(new Size(width, height)));

    image.Metadata.ExifProfile = null;

    image.SaveAsJpeg(name, new JpegEncoder
    {
        Quality = 100
    });
}

This is the function to create and get the temporary folder:这是 function 创建和获取临时文件夹:

private string TemporaryFolder()
{
    DirectoryInfo result = Directory.CreateDirectory(Path.GetTempPath() + "TemporaryImages/");

    return result.FullName;
}

First of all, it looks like you are not specifying any file name when saving the file.首先,看起来您在保存文件时没有指定任何文件名。 There are a few options on what filenames to use:关于要使用的文件名有几个选项:

  1. Use numbers, check current files to avoid duplicates.使用数字,检查当前文件以避免重复。
  2. Use Guids, they are guaranteed to be unique使用Guids,它们保证是唯一的
  3. Use Path.GetTempFileName , this will give a full filename in the root of the temp folder.使用Path.GetTempFileName ,这将在临时文件夹的根目录中提供完整的文件名。

Secondly, if you are running asp.net you likely do not have write-permission to the temp folder.其次,如果您运行的是 asp.net,您可能没有临时文件夹的写权限。 You probably do not want to grant your web-process access to your systems temp-folder since that might have security implications.您可能不想授予您的 Web 进程访问系统临时文件夹的权限,因为这可能会产生安全隐患。

If the files are truly temporary , as in only valid during a single request, I would recommend keeping them in memory.如果文件是真正的临时文件,例如仅在单个请求期间有效,我建议将它们保存在 memory 中。 Images can be saved to a memory stream just fine, and this avoids any issue with cleanup etc. The main reason to write such temporary data to files is either memory constraints, and this should be fairly rare today, or some API that require actual files. Images can be saved to a memory stream just fine, and this avoids any issue with cleanup etc. The main reason to write such temporary data to files is either memory constraints, and this should be fairly rare today, or some API that require actual files .

If you are rather want a short lived image storage I would suggest following the standard practices for storing images, ie use a database to keep a list of images, a configurable storage folder with correct permissions to store images, and some cleanup process to periodically remove unneeded images, etc.如果您想要一个短暂的图像存储,我建议您遵循存储图像的标准做法,即使用数据库来保存图像列表、具有正确存储图像权限的可配置存储文件夹以及一些定期删除的清理过程不需要的图像等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM