問題描述
所以我正在為我的家人創建一個網站,我們可以在其中上傳我們的圖像并查看它們,但該網站的一個重要功能是按日期排序,以便例如當我的阿姨在我母親的生日和我也拍了照片,我們上傳它們將添加到同一個相冊等的圖像.
我意識到通過瀏覽器上傳時無法保留日期.所以我會做一個只用于上傳圖片的小程序.我有一個 FTP 服務器正在運行,但是當我上傳圖像時,日期將更改為當前日期時間.我找到了為什么這樣做的答案 所以現在我正在尋找一種在上傳到 FTP 時保留日期的方法.
以下是我的一些想法:
- 如果程序將文件添加到 zip 文件并上傳該 zip 文件,它們將保留日期,但這意味著我必須在服務器上安裝一些東西來解壓 zip.
- 上傳圖片后,程序會從原始圖片中提取創建日期,并將其添加到一個文本文件中,該文件也會上傳,但這需要服務器上的程序更改上傳的圖片創建日期.李>
- 也許我上傳了圖片,然后從客戶端更改了上傳圖片的創建日期?
- 也許我上傳了圖片,然后從客戶端更改了上傳圖片的創建日期?
在 FTP 協議中,使用 MFMT
或 MDTM
命令更新文件修改時間戳,或 MFCT
更新文件創建時間戳,具體取決于哪個您的 FTP 服務器支持的這些.
實際上沒有一個是標準化的.
MFMT
和MFCT
在此起草:
https://datatracker.ietf.org/doc/html/草稿-somers-ftp-mfxx-04MDTM
在 RFC 3659 中定義為檢索文件修改時間戳,使用MDTM 文件名
語法.但許多 FTP 服務器也支持替代(非標準)語法MDTM 文件名時間戳
(即與提議的MFMT
相同)來更新修改時間戳.
盡管 .NET 框架中的本機 FTP 實現(FtpWebRequest
或 WebClient
包裝器)不支持其中任何一個.
您必須使用第 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:
- 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.
- 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.
- Maybe I upload the images and thereafter change the uploaded images created date from the client?
- 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
andMFCT
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, usingMDTM filename
syntax. But many FTP servers support an alternative (non-standard) syntaxMDTM filename timestamp
(i.e. the same as proposedMFMT
) 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 時保留圖像創建日期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!