gpt4 book ai didi

C# WebClient UploadFile 给我一个 PathTooLongException(它不会太长!)

转载 作者:行者123 更新时间:2023-11-30 21:19:05 24 4
gpt4 key购买 nike

从 C# 将文件上传到 aspx 页面。获得:

PathTooLongException
The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

代码如下:

            try
{
using (var client = new WebClient())
{
String url =
String.Format(
"http//localhost:49536/ManualUploadTest.aspx?key={0}&name={1}&address={2}&phone={3}&email={4}&node={5}",
"changeme",
"john",
"10 Downing Street",
"555 555 6165",
"test@yahoo.com",
"TestNode");

var len = url.Length; // this length is 146
var encodeLen = HttpUtility.UrlEncode(url).Length; // this length is 180

//client.BaseAddress = "http//localhost:49536";

byte[] result = client.UploadFile(HttpUtility.UrlEncode(url), path);

// throws exception during UploadFile
// ... more code here

url 字符串如下所示:

http//localhost:49536/ManualUploadTest.aspx?key=changeme&name=john&address=10 Downing Street&phone=555 555 6165&email=test@yahoo.com&node=TestNode

路径是:

Y:\\10mb.zip

感谢您的帮助!

最佳答案

尝试修复 URL:http://... 而不是 http//...;另外,我会使用 Uri 类而不是 UrlEncode()

Uri url = new Uri(String.Format("http://localhost:49536/ManualUploadTest.aspx?key={0}&name={1}&address={2}&phone={3}&email={4}&node={5}",
HttpUtility.UrlEncode("changeme"),
HttpUtility.UrlEncode("john"),
HttpUtility.UrlEncode("10 Downing Street"),
HttpUtility.UrlEncode("555 555 6165"),
HttpUtility.UrlEncode("test@yahoo.com"),
HttpUtility.UrlEncode("TestNode")));
byte[] result = client.UploadFile(url, path);

编辑:我找到了异常的原因...如果您提供一个字符串,它会尝试在内部创建一个 Uri(由于格式错误的协议(protocol) http// 而失败),然后它尝试使用 Path.GetFullPath(url) 获取 Uri 的完整路径,然后失败并返回 PathTooLongException

关于C# WebClient UploadFile 给我一个 PathTooLongException(它不会太长!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3955086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com