跳到正文
DEV Community· MinPin Proxy·· 17 小時前AI 評分57

如何讓大型檔案上傳在行動網絡中斷後續傳

The upload works in the office and dies at 80% on the train. Making large uploads survive mobile networks.

AI 導讀

MinPin Proxy 説明如何讓大型檔案上傳在行動網絡中斷後續傳,避免上傳失敗後由零開始。文章建議先排查伺服器、CDN 與用戶端的大小及逾時限制,再採用 tus、S3 multipart upload 或 Google Cloud Storage resumable uploads,讓用戶端從已完成的位置繼續傳送。

正文

The bug: users attach a video or a batch of photos, the progress bar gets most of the way, and then the upload fails and starts again from zero. On office Wi-Fi it never happens. On a phone it happens a lot, because a long upload on a mobile network is very likely to hit at least one of these: a network switch (Wi-Fi to cellular), a dead zone, the user leaving the app, or a timeout somewhere between the phone and your server.

A single POST with the whole file in the body has no way to recover from any of them. Here's what does.

1. Find out where it actually fails

Before changing the client, check the path the upload takes. A few limits are hit only by large or slow uploads, and they look like network failures from the phone:

  • nginx client_max_body_size defaults to 1 MB. Larger bodies get 413 Request Entity Too Large, sometimes before the client has finished sending, which some clients report as a broken connection instead of a 413.
  • nginx client_body_timeout (default 60 seconds) closes the connection if the client sends nothing for that long. A phone in a tunnel easily does that.
  • CDNs and load balancers have their own body size and idle limits. Some plans cap request bodies at 100 MB no matter what your server allows.
  • Your app server's own limit (body parser size, multipart limit, request timeout).

Test by uploading a file just over each limit from a fast connection. If that fails, the problem isn't the mobile network.

2. Get the timeouts right on the client

On iOS, URLSessionConfiguration.timeoutIntervalForRequest (default 60 s) is an idle timeout: it fires when no data moves for that long, not when the whole request takes longer than 60 s. timeoutIntervalForResource (default 7 days) is the total limit. Setting the request timeout to a few minutes "because uploads are slow" mostly delays the error. Leave it, and make the upload resumable instead.

On Android with OkHttp, writeTimeout is also per write, not for the whole body. callTimeout is the total, and it's off by default. If someone set callTimeout to 30 seconds for the whole app, every upload that takes longer than 30 seconds fails, and that only happens on slow networks.

3. Make the upload resumable

The fix that actually solves it: upload in pieces, and let the client ask the server how much it already has.

You don't need to design this yourself. Common options:

  • tus, an open protocol for resumable uploads with server and client libraries for most stacks. The client creates an upload, sends the file with PATCH requests carrying an Upload-Offset header, and after a failure sends HEAD to learn the offset the server has and continues from there.
  • S3 multipart upload: split the file into parts (at least 5 MiB each, except the last; at most 10,000 parts), upload each part with its own presigned URL, then complete the upload. A failed part is retried on its own. Remember to abort or expire unfinished uploads, because their parts are stored and billed.
  • Google Cloud Storage resumable uploads: start a session, upload chunks, and query the session for the committed size after an interruption.

Whatever you use, the client has to store the upload ID and the part list on disk, not in memory, so it can continue after the app is killed.

4. Keep uploading when the user leaves the app

iOS: use a background URLSession (URLSessionConfiguration.background(withIdentifier:)) with uploadTask(with:fromFile:). Background sessions only upload from files, so write the data (or each part) to a file first. The system carries on with the upload while your app is suspended or terminated by the system, and relaunches the app in the background when it's done. (If the user force-quits the app, iOS cancels its background transfers, which is another reason to keep the upload state on disk and resume on the next launch.) Implement application(_:handleEventsForBackgroundURLSession:completionHandler:) and call the completion handler after processing the events. Each part of a multipart upload can be its own background upload task.

Android: use WorkManager with a NetworkType.CONNECTED constraint so the work waits for a network and retries with backoff. For long uploads the user started, run it as a foreground worker with a progress notification (setForeground); on Android 14 and later that foreground service needs the dataSync type declared. Android 14 also added user-initiated data transfer jobs in JobScheduler, made for exactly this case.

5. Send less

The cheapest fix: a 12-megapixel photo doesn't need to go up at full size if the server shrinks it anyway. Resize and recompress on the device first. For video, check whether the server transcodes it anyway, and if it does, upload a lower bitrate version.

Testing it

  • Use a slow network profile (on a Mac, Network Link Conditioner's "3G" or "Very Bad Network") and start a large upload.
  • Halfway through, turn on airplane mode for 30 seconds, then turn it off. The upload should continue from where it was, not from 0%.
  • Start an upload, send the app to the background, and wait: it should finish without the app in front. Then force-quit it during an upload and open it again: it should pick up from the stored offset.
  • Watch the requests: after an interruption you should see the "how much do you have?" request (HEAD for tus, list parts for S3) followed by only the missing bytes.

This article was created with the help of AI.

來源:DEV Community · dev.to