2022-10-29 11:01:23 +00:00
|
|
|
---
|
|
|
|
module: [kind=event] file_transfer
|
2022-10-29 21:49:45 +00:00
|
|
|
since: 1.101.0
|
2022-10-29 11:01:23 +00:00
|
|
|
---
|
|
|
|
|
2023-03-15 21:52:13 +00:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: 2022 The CC: Tweaked Developers
|
|
|
|
|
|
|
|
SPDX-License-Identifier: MPL-2.0
|
|
|
|
-->
|
|
|
|
|
2023-08-24 09:48:30 +00:00
|
|
|
The [`file_transfer`] event is queued when a user drags-and-drops a file on an open computer.
|
2022-10-29 11:01:23 +00:00
|
|
|
|
2023-08-24 09:48:30 +00:00
|
|
|
This event contains a single argument of type [`TransferredFiles`], which can be used to [get the files to be
|
|
|
|
transferred][`TransferredFiles.getFiles`]. Each file returned is a [binary file handle][`fs.BinaryReadHandle`] with an
|
|
|
|
additional [getName][`TransferredFile.getName`] method.
|
2022-10-29 11:01:23 +00:00
|
|
|
|
|
|
|
## Return values
|
2023-08-24 09:48:30 +00:00
|
|
|
1. [`string`]: The event name
|
|
|
|
2. [`TransferredFiles`]: The list of transferred files.
|
2022-10-29 11:01:23 +00:00
|
|
|
|
|
|
|
## Example
|
|
|
|
Waits for a user to drop files on top of the computer, then prints the list of files and the size of each file.
|
|
|
|
|
|
|
|
```lua
|
|
|
|
local _, files = os.pullEvent("file_transfer")
|
|
|
|
for _, file in ipairs(files.getFiles()) do
|
|
|
|
-- Seek to the end of the file to get its size, then go back to the beginning.
|
|
|
|
local size = file.seek("end")
|
|
|
|
file.seek("set", 0)
|
|
|
|
|
|
|
|
print(file.getName() .. " " .. file.getSize())
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
## Example
|
|
|
|
Save each transferred file to the computer's storage.
|
|
|
|
|
|
|
|
```lua
|
|
|
|
local _, files = os.pullEvent("file_transfer")
|
|
|
|
for _, file in ipairs(files.getFiles()) do
|
|
|
|
local handle = fs.open(file.getName(), "wb")
|
|
|
|
handle.write(file.readAll())
|
|
|
|
|
|
|
|
handle.close()
|
|
|
|
file.close()
|
|
|
|
end
|
|
|
|
```
|