Skip to content
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

Filename encoding issue in latest version. #2117

Closed
kashifjaved1 opened this issue Jul 18, 2023 · 2 comments · Fixed by #2123
Closed

Filename encoding issue in latest version. #2117

kashifjaved1 opened this issue Jul 18, 2023 · 2 comments · Fixed by #2123
Labels

Comments

@kashifjaved1
Copy link

kashifjaved1 commented Jul 18, 2023

I'm using the latest stable version of restsharp (v110.2.0) to upload zip file to external link with name "Präsentation_Export" but after successful upload got "Pr�sentation_Export" from restsharp. I had done this work successfully working in version 105.2.3 but in v110.2.0 its not working.
Below is my controller method:

public async Task<ApiResponse> UploadRequest(int? Id, IFormFile file, string APIEndPoint, string MethodType)
{
    ApiResponse result = new ApiResponse();
    try
    {
        var queryString = new Dictionary<string, string>() { { "access_token", AccessToken } };
        Uri uri = new Uri(string.Format(BaseURL + APIEndPoint, string.Empty));
        var requestUri = QueryHelpers.AddQueryString(uri.ToString(), queryString);

        using var memoryStream = new MemoryStream();
        var client = new RestClient(uri);
        await file.CopyToAsync(memoryStream);
        //var restRequest = new RestRequest(requestUri, Method.Post);
        restRequest.AddHeader("Content-Type", "multipart/form-data");
        var restRequest = new RestRequest("https://tpsupport.tpondemand.com/", Method.Post)
            .AddFile("attachment", memoryStream.ToArray(), file.FileName, file.ContentType, options: new FileParameterOptions { DisableFilenameEncoding = true });

        if (Id != null)
        {
            restRequest.AddParameter("generalId", Id.Value);
        }

        var response = await client.ExecuteAsync(restRequest);

        string resultContent = response.Content;
        if (response.IsSuccessStatusCode)
        {
            result.Data = resultContent;
        }
        else
        {
            result.ExceptionObject = JsonConvert.DeserializeObject<Exception>(resultContent);
        }
        result.StatusCode = (int)response.StatusCode;
        return result;
    }
    catch (Exception ex)
    {
        result.ExceptionObject = ex;
        result.StatusCode = 500;
        return result;
    }
}

Desktop:

OS: Windows 11
.NET version 4.7.2
Version 110.2.0

Additional Information:
I'm uploading file with name "Präsentation_Export.zip", but after successful upload getting it with name "Pr�sentation_Export.zip".

@alexeyzimarev
Copy link
Member

I believe the issue is here:

if (!fileParameter.Options.DisableFileNameStar) dispositionHeader.FileNameStar = fileParameter.FileName;

RestSharp should set FileNameStar to the encoded filename, not the raw string.

@alexeyzimarev
Copy link
Member

Ok, I added a test and found out that it's not a bug and my PR doesn't really fix anything, it breaks things.

What you need to do is to enable fileName* by setting DisableFileNameStar to false (it's true by default).

alexeyzimarev added a commit that referenced this issue Aug 14, 2023
* Use encoded filename for FileNameStar (#2117)

* Remove the "fix" and add a test that shows it working

* Update the docs

* Fix the CsvHelper new version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants