与RESTSHARP文档相反,请求。ADDFILE不接受fileeparameter作为Addfile的参数。可以找到有关如何迁移的任何文档。有什么想法吗?

发布于 2025-02-10 10:25:47 字数 501 浏览 3 评论 0 原文

好的,这是自从我将RestSharp更新为当前版本以来的C#代码:

restRequest.Files.Add((FileParameter)file); // Added cast for clarity

根据,这是正确的选择:

restRequest.AddFile((FileParameter)file);

但是,这似乎是不正确的,因为它引发了此例外:

方法“ addfile”没有超载

这是1个参数直接与RestSharp文档相矛盾。我找不到任何方法来解决这个问题。我尝试了那里的过载,但是FileParameter不再包含所需的作家属性,我找不到任何文档提及移至何处的文档。

我想念什么?

Ok so here's the C# code that's breaking since I updated RestSharp to the current version:

restRequest.Files.Add((FileParameter)file); // Added cast for clarity

According to the the current official documentation, this is the correct alternative:

restRequest.AddFile((FileParameter)file);

However, that appears to be incorrect, as it throws this exception:

No overload for method 'AddFile' takes 1 arguments

This directly contradicts the RestSharp docs. I haven't been able to find any way to get around this. I tried the overloads that are there but FileParameter no longer contains the Writer property which would be required and I can't find any docs mentioning where that was moved to.

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一枫情书 2025-02-17 10:25:47

restrequest.files READONLY集合因此,您无法使用该属性添加更多文件。

由于Overload Restrequest.addfile(((fileparameter)文件); 在较新版本的RestSharp中不存在,因此您可以定义自己的扩展方法:

using RestSharp;

public static class RestRequestExtensions
{
    public static RestRequest AddFile(this RestRequest req, FileParameter f)
    {
        return req.AddFile(f.Name, GetBytes(f.GetFile()), f.FileName, f.ContentType);
    }

    private static byte[] GetBytes(Stream input)
    {
        byte[] buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }
}

另一种方法:另一种方法:

public static class RestRequestExtensions
{
    public static RestRequest AddFile(this RestRequest req, FileParameter f)
    {
        return req.AddFile(f.Name, ()=>f.GetFile(), f.FileName, f.ContentType);
    }
}

restRequest.Files is readonly collection so you cannot use that property to add more files.

readonly restsharp collection files

Since the overload restRequest.AddFile((FileParameter)file); is not there in the newer version of restsharp, you can define your own extension method like this:

using RestSharp;

public static class RestRequestExtensions
{
    public static RestRequest AddFile(this RestRequest req, FileParameter f)
    {
        return req.AddFile(f.Name, GetBytes(f.GetFile()), f.FileName, f.ContentType);
    }

    private static byte[] GetBytes(Stream input)
    {
        byte[] buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }
}

Another way:

public static class RestRequestExtensions
{
    public static RestRequest AddFile(this RestRequest req, FileParameter f)
    {
        return req.AddFile(f.Name, ()=>f.GetFile(), f.FileName, f.ContentType);
    }
}
哀由 2025-02-17 10:25:47

我也遇到了这个问题(RestSharp 108.0.1),同时使用API​​集成。我能够通过将文件添加到请求正文中来解决问题。

byte[] fileBytes = File.ReadAllBytes({your filePath});

//request setup...

restRequest.AddBody(fileBytes, {your fileType});

I just came across this issue as well (restsharp 108.0.1) while working with an api integration. I was able to get around the issue by adding my file into the request body.

byte[] fileBytes = File.ReadAllBytes({your filePath});

//request setup...

restRequest.AddBody(fileBytes, {your fileType});
早茶月光 2025-02-17 10:25:47

addfile(fileparameter)已被标记为@serg注释的内部,因此 request> request.addfile(fileparameter); 将在以后无法使用版本除非他们再次删除内部修饰符。

至于 Action< stream> writer fileparameter的属性,现在变成 func< stream> getfile 。要指定您自己的 getfile ,您可以使用指定的 getfile 来调用内部addfile(fileparameter)来调用此

AddFile(parameterName, getFile, fileName, contentType);

方法

RestRequest.AddFile(FileParameter.Create(name, getFile, fileName, contentType))

扩展 有点混淆旧版本是 action&lt; stream&gt; 库将 stream 将传递给您的 writer ,现在您需要传递<<<代码>流通过 getfile 到库。

The AddFile(FileParameter) has been marked as internal as commented by @Serg, so request.AddFile(fileParameter); will not work in later version unless they remove the internal modifier again.

As to the Action<Stream> Writer property of FileParameter, now it becomes Func<Stream> GetFile. To specify your own GetFile, you could call this extension method

AddFile(parameterName, getFile, fileName, contentType);

which will call the internal AddFile(FileParameter) using the specified GetFile:

RestRequest.AddFile(FileParameter.Create(name, getFile, fileName, contentType))

It would be a bit confusing that old versions is Action<Stream> where the library will pass the Stream to your Writer and now you need to pass the Stream to the library via GetFile.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文