Thanks to visit codestin.com
Credit goes to github.com

Skip to content

HttpClient not support upload file with chinese name #41249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
krissss opened this issue May 17, 2021 · 6 comments
Closed

HttpClient not support upload file with chinese name #41249

krissss opened this issue May 17, 2021 · 6 comments

Comments

@krissss
Copy link

krissss commented May 17, 2021

Symfony version(s) affected: 4.4.13

Description

HttpClient upload file with chinese name, but server cannot accept it.

How to reproduce

client:

$client = HttpClient::create();
$formData = new FormDataPart([
    'files' => DataPart::fromPath(__DIR__ . '/中文.txt')
]);
$response = $client->request('POST', 'http://localhost/test/fileupload.php', [
    'headers' => $formData->getPreparedHeaders()->toArray(),
    'body' => $formData->bodyToIterable(),
]);
dd($response->getContent(false));

server:

var_dump($_FILES);

The right result should have $_FILES, but no.

Possible Solution

I log formData info use this: file_put_contents(__DIR__ . '/a.txt', $formData->toString());, and get

Content-Type: multipart/form-data; boundary=RGWgOk4l

--RGWgOk4l
Content-Type: text/plain
Content-Transfer-Encoding: 8bit
Content-Disposition: form-data; name="files"; filename*=utf-8''"%E4%B8%AD%E6%96%87.txt"

中文
--RGWgOk4l--

When I change
Content-Disposition: form-data; name="files"; filename*=utf-8''"%E4%B8%AD%E6%96%87.txt"
to Content-Disposition: form-data; name="files"; filename="中文.txt"
or Content-Disposition: form-data; name="files"; filename="中文.txt"; filename*=utf-8''"%E4%B8%AD%E6%96%87.txt"
It correct uploaded!

But I dont known how to fix this

Additional context

@nicolas-grekas
Copy link
Member

nicolas-grekas commented May 19, 2021

Thanks for the report. This is issue is for the Mime component, which is the one that creates the Content-Disposition header.
Generating only a filename*= looks like the issue to me.

When submitting this file, neither Chrome nor Firefox generate the filename*= variant, although it's the only standard form.
Chrome generates filename="中文.txt" and Firefox filename="\u4e2d\u6587.txt".

I guess we should start accepting UTF-8 in filename=, since Chrome does it anyway.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented May 19, 2021

We should also give feedback at https://trac.ietf.org/trac/httpbis/wiki/ContentDispositionProducers

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Could I get an answer? If I do not hear anything I will assume this issue is resolved or abandoned. Please get back to me <3

@carsonbot
Copy link

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

@krissss
Copy link
Author

krissss commented Dec 20, 2021

Not solved

@fancyweb fancyweb reopened this Dec 20, 2021
@fabpot fabpot closed this as completed Dec 20, 2021
fabpot added a commit that referenced this issue Dec 20, 2021
…las-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[Mime] Fix encoding filenames in multipart/form-data

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #41249
| License       | MIT
| Doc PR        | -

File uploads that use forms have stick to browsers' behavior. This is captured in the WHATWG in their living standard:
https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#multipart-form-data

curl did the same a few weeks ago:
curl/curl#7805

Commits
-------

a58c342 [Mime] Fix encoding filenames in multipart/form-data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants