简体   繁体   English

如何从 C# 中的 Headers 中检索值?

[英]How to retrieve the value from Headers in C#?

I have a variable fileContent which holds the value of FileName.我有一个变量 fileContent,它保存 FileName 的值。 I want the filename value to be stored in separate variable.我希望将文件名值存储在单独的变量中。 How can I do this using C#?如何使用 C# 执行此操作?

在此处输入图像描述

Please find below code: This is what I have tried:请找到以下代码:这是我尝试过的:

var fileContent = provider.Contents.FirstOrDefault(c => c.Headers.ContentDisposition.Name.Replace("\"", "") == "file");

var fileName = fileContent.Headers.ContentDisposition.FileName.FirstOrDefault();

I am getting the integer value as 34 in the variable fileName - instead of "Test.txt".. Please help to fix this!我在变量文件名中得到的 integer 值为 34,而不是“Test.txt”。请帮助解决这个问题!

don't treat Filename as an array and leave out the.FirstOrDefault().不要将 Filename 视为数组并省略 the.FirstOrDefault()。 The 34 is probably the ascii value of the opening quote in the file name. 34 可能是文件名中左引号的 ascii 值。

Personally I never use var .我个人从不使用var Replace with the actual type you need, and you will see a better error message.替换为您需要的实际类型,您将看到更好的错误消息。

To remove the quotes, you might try to call something like Trim('"'). I don't have C# here to test though:-(要删除引号,您可以尝试调用类似 Trim('"') 的方法。不过我这里没有 C# 来测试:-(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM