","image":"http://www.luping.net/uploads/20241104/17307198076728b03faccae.jpg","datePublished":"2024-11-08T22:31:00+08:00","dateModified":"2024-11-08T22:31:00+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Read and Echo Currently Writing Size of Uploaded File in Server in Realtime Without Blocking Server and Client?

How to Read and Echo Currently Writing Size of Uploaded File in Server in Realtime Without Blocking Server and Client?

Published on 2024-11-08
Browse:970

How to Read and Echo Currently Writing Size of Uploaded File in Server in Realtime Without Blocking Server and Client?

How to read and print the size of an uploaded file being written on the server side in real time without blocking the server and client?

Let us expand on this issue:

In order to get the progress of file upload in real time, we set it from the Blob, File, TypedArray or ArrayBuffer object through fetch() in the POST request The body object.

The current implementation sets the File object as the second argument to fetch() passed to the body object.

Requirements:

as text/event-stream reads the size of the file being written to the server's file system and echoes it back to the client. Stops when all bytes provided by the var query string parameter in the GET request have been written. File reading currently happens in a separate scripting environment, where GET calls are made to the script that reads the file, and then POST is made to the script that writes the file to the server.

Wait until you complete the file size echo section before trying to resolve potential issues with handling server-side file writes or file reads to get the current file size.

Currently trying to use php to meet the requirements. But also interested in c, bash, nodejs, python or other languages ​​or methods that can be used to perform the same task.

The client-side javascript part is fine. I'm just not well versed in php (one of the most widely used server-side languages ​​in the world) to implement the pattern without including unnecessary parts.

Motivation:

Progress indicator for fetch?

Related:

Fetch with ReadableStream

Problem:

Get

PHP Notice:  Undefined index: HTTP_LAST_EVENT_ID in stream.php on line 7

in the terminal.

Additionally, if

while(file_exists($_GET["filename"]) 
  && filesize($_GET["filename"]) < intval($_GET["filesize"]))

is replaced with

while(true)

It will generate an error at the EventSource.

Without the sleep() call, the correct file size is dispatched to the message event for a file of size 3.3MB, 3321824, 61921, 26214 and 38093 are printed respectively when the same file is uploaded three times. The expected result is to get the file size when writing a file at:

stream_copy_to_stream($input, $file);

instead of the filesize of the uploaded file object. Will fopen() or stream_copy_to_stream() prevent other php processes from accessing stream.php?

Things tried so far:

php Quote from

  • Beyond $_POST, $_GET and $_FILE: In JavaScriptPHP Processing Blob
  • Server-Sent Events Introduction with PHP Example

php

// 能否合并 `data.php`、`stream.php` 为同一个文件?
// 能否使用 `STREAM_NOTIFY_PROGRESS` 
// "Indicates current progress of the stream transfer 
// in bytes_transferred and possibly bytes_max as well" to read bytes?
// do we need to call `stream_set_blocking` to `false`
// data.php
// stream.php

javascript









Release Statement This article is reprinted at: 1729432843 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3