上传到 FTP 时保留图像创建日期

Preserve image created date when uploading to FTP(上传到 FTP 时保留图像创建日期)

本文介绍了上传到 FTP 时保留图像创建日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在为我的家人创建一个网站,我们可以在其中上传我们的图像并查看它们,但该网站的一个重要功能是按日期排序,以便例如当我的阿姨在我母亲的生日和我也拍了照片,我们上传它们将添加到同一个相册等的图像.

我意识到通过浏览器上传时无法保留日期.所以我会做一个只用于上传图片的小程序.我有一个 FTP 服务器正在运行,但是当我上传图像时,日期将更改为当前日期时间.我找到了为什么这样做的答案 所以现在我正在寻找一种在上传到 FTP 时保留日期的方法.

以下是我的一些想法:

  1. 如果程序将文件添加到 zip 文件并上传该 zip 文件,它们将保留日期,但这意味着我必须在服务器上安装一些东西来解压 zip.
  2. 上传图片后,程序会从原始图片中提取创建日期,并将其添加到一个文本文件中,该文件也会上传,但这需要服务器上的程序更改上传的图片创建日期.
  3. 也许我上传了图片,然后从客户端更改了上传图片的创建日期?

解决方案

  1. 也许我上传了图片,然后从客户端更改了上传图片的创建日期?

在 FTP 协议中,使用 MFMTMDTM 命令更新文件修改时间戳,或 MFCT 更新文件创建时间戳,具体取决于哪个您的 FTP 服务器支持的这些.

实际上没有一个是标准化的.

  • MFMTMFCT 在此起草:
    https://datatracker.ietf.org/doc/html/草稿-somers-ftp-mfxx-04
  • MDTM 在 RFC 3659 中定义为检索文件修改时间戳,使用 MDTM 文件名 语法.但许多 FTP 服务器也支持替代(非标准)语法 MDTM 文件名时间戳(即与提议的 MFMT 相同)来更新修改时间戳.

尽管 .NET 框架中的本机 FTP 实现(FtpWebRequestWebClient 包装器)不支持其中任何一个.

您必须使用第 3 方库.

例如,WinSCP .NET 程序集会自动为任何上传(或下载),无需任何额外代码.

上传文件的简单示例代码(隐式保留修改时间戳):

//设置会话选项SessionOptions sessionOptions = 新的 SessionOptions{协议 = Protocol.Ftp,主机名 = example.com",用户名 = 用户",密码=我的密码",};使用(会话会话 = 新会话()){//连接session.Open(sessionOptions);//上传session.PutFiles(@"d:	ouploadimage.jpg", "/home/user/").Check();}

有关详细信息,请参阅 Session.PutFiles.p>

最新版本的 WinSCP GUI FTP 客户端(5.9 及更高版本)甚至可以生成 C# 代码给你.

(我是 WinSCP 的作者)

So I am making a website for my family, where we can upload our images and view them, but an important feature of the website is to sort by date so that when for example my aunt have taken pictures at my mothers birthday and I also have taken pictures and we upload the images they will be added to the same album etc.

I've realized that it is not possible to preserve the date, when uploading through a browser. So I will make a small program which is only used for upload pictures. I have an FTP server running but when ever I upload images the date will change to current datetime. I have found the answer to why it does that so now I am looking for a way to preserve the date while uploading to FTP.

Here's some ideas I've had:

  1. If the program adds the files to a zip file and upload that zip file they will preserve the date, but that means I would have to have something on the server that unpacks the zips.
  2. When the images gets uploaded the program extracts the created date from the original image and adds it to a text file which it also uploads, but that would again require a program on the server which changes the uploaded images created date.
  3. Maybe I upload the images and thereafter change the uploaded images created date from the client?

解决方案

  1. Maybe I upload the images and thereafter change the uploaded images created date from the client?

In FTP protocol, use MFMT or MDTM command to update file modification timestamp, or MFCT to update file creation timestamp, depending on which of these your FTP server supports.

Actually none of them is standardized.

  • The MFMT and MFCT are drafted here:
    https://datatracker.ietf.org/doc/html/draft-somers-ftp-mfxx-04
  • The MDTM is defined in RFC 3659 to retrieve file modification timestamp, using MDTM filename syntax. But many FTP servers support an alternative (non-standard) syntax MDTM filename timestamp (i.e. the same as proposed MFMT) to update the modification timestamp too.

Though the native FTP implementation in .NET framework (the FtpWebRequest or the WebClient wrapper), does not support any of these.

You have to use a 3rd party library.

For example the WinSCP .NET assembly preserves the modification timestamp automatically for any upload (or download) without any additional code.

A simple example code to upload a file (implicitly preserving the modification timestamp):

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "example.com",
    UserName = "user",
    Password = "mypassword",
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Upload
    session.PutFiles(@"d:	ouploadimage.jpg", "/home/user/").Check();
}

For details, see Session.PutFiles.

The latest version of WinSCP GUI FTP client (5.9 and newer) can even generate the C# code for you.

(I'm the author of WinSCP)

这篇关于上传到 FTP 时保留图像创建日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:上传到 FTP 时保留图像创建日期

基础教程推荐