【问题标题】:How to save the response in the Postman same as the name I mentioned it?如何在邮递员中保存与我提到的名称相同的响应?
【发布时间】:2021-06-02 19:33:17
【问题描述】:

这段代码会将所有数据(字符串)作为 XML 格式放入 Postman。但是当我尝试在 Postman 中保存响应时,它会保存为 response.xml,这是我不想要的。我希望 Postman 以 filename.xml 的形式保存我的 XML 内容。

那么,我该怎么做呢?我必须用谷歌搜索它,发现必须使用附件、内容类型之类的东西。但是,并没有解决我的问题。请帮忙提出您的建议。

为简单起见,我减少了代码。这里,graphicsstring 数据类型。

public ActionResult GetXML()
{
    string filename = "Demo";
    return Content(graphics, "application/xml");
}

【问题讨论】:

  • 所以您希望GetXML 操作像文件下载一样起作用?如果不是,我不明白问题出在哪里。
  • 是的,完全正确。我希望将 GetXML 视为文件下载。但是文件名不应该像 response.xml (默认情况下在 Postman 中)。我想要文件名.xml
  • @Ackdari 你明白了吗?我想说什么。我希望邮递员将保存/下载名为 filename.xml 的文件,而不是 response.xml。
  • 我认为您无法从服务器代码中控制邮递员可以做什么或不做什么。来自 API 的响应被加载到邮递员中,它不是邮递员的本地内容,所以当用户想要保存该响应是邮递员的内部设置来决定保存时的默认文件名,但用户始终可以在“另存为”对话框中更改文件名...顺便说一句,为什么要控制邮递员使用的文件名?
  • @ChetanRanpariya 谢谢。那么你怎么能告诉我应该如何将这个 XML 数据作为一个 XML 文件返回,该文件应该在 Web 浏览器中自动下载。你能不能给它写代码。只需将此 XML 数据转换为文件形式,并在客户端收到请求时在 Web 浏览器中自动下载。

标签: c# asp.net asp.net-mvc asp.net-web-api postman


【解决方案1】:

我建议使用FileContentResult 进行文件下载。

public ActionResult GetXML()
{
    var fileName = "Demo.xml";
    var xml = "...";

    return new FileContentResult(Encoding.UTF8.GetBytes(xml), "application/xml; charset=utf-8")
    {
        FileDownloadName = fileName,
    };
}

【讨论】:

    【解决方案2】:

    您需要设置Content-Disposition 标头,以便浏览器提示保存响应。

    您可以使用以下方法来做到这一点。

    public IActionResult Getxml()
    {
            var xmlData =
                "<records><record><Name>Camacho, Sydnee Q.</Name><Id>1</Id><Age>19</Age><City>Podolsk</City></record><record><Name>Bowman, Lester V.</Name><Id>2</Id><Age>21</Age><City>Padang</City></record></records>";
    
            //sampleFile.xml can be replaced by any filename of your choice.
            var fileName = "sampleFile.xml";
            Response.Headers.Add("Content-Disposition", $"attachment; filename={fileName}");
            return Content(xmlData, "application/xml", Encoding.UTF8);
    }
    

    通过此更改,当在浏览器中浏览此 URL 时,它将提示保存对话框,其中 sampleFile.xml 填充为文件名。

    当您保存来自邮递员的回复时,它会在对话框中显示sampleFile.xml 填充为文件名。

    希望这能帮助您解决问题。

    【讨论】:

    • 非常感谢!!!......我喜欢它。这正是我想要的……谢谢。
    【解决方案3】:

    您需要设置响应头,以便下载客户端自动将内容视为“文件”,关键是ContentDisposition 头,这就是我们将文件名传回的方式。

    public HttpResponseMessage GetXMLFile()
    {
        string filename = "Demo";
        var byteArray = System.Text.Encoding.UTF8.GetBytes(graphics);
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new StreamContent(new MemoryStream(byteArray), byteArray.Length)
        };
    
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") //attachment will force download
        {
            FileName = filename
        };
    
        return result;
    }
    

    【讨论】:

    • 非常感谢您的代码。但是我已经粘贴了这段代码,之前我也做了同样的事情,但不知何故,它没有下载。
    猜你喜欢
    • 2020-01-27
    • 2021-12-18
    • 2021-01-27
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 2018-03-26
    相关资源
    最近更新 更多