WebClient DownloadFile中路径存在非法字符

4

我是新手,所以我确定我错过了一些基本的东西。

我有一个简单的程序来运行一个包含图像链接的csv文件,将这些图像保存在指定的保存文件位置中。

我正在将包含url的单元格解析为List<string[]>

如果我输入 GetImage(@"http://www.example.com/picture.jpg", 1),我的GetImage函数会按照预期执行。但当我尝试使用循环并传递str [0]变量时,我会收到关于路径中非法字符的错误。

我已经使用MessageBox告诉我区别所在,据我所知,当我将str[0]传递给函数时,它会添加双引号(例如,"http://www.example.com"被显示,而不是http://www.example.com,就像我只发送一个字符串时一样)。

我做错了什么?

private void button2_Click(object sender, EventArgs e)
    {
        string fileName = textBox1.Text;            
        folderBrowserDialog1.ShowDialog();
        string saveLocation = folderBrowserDialog1.SelectedPath;
        textBox2.Text = saveLocation;            
        List<string[]> file = parseCSV(fileName);
        int count = 0;
        foreach (string[] str in file)
        {
            if (count != 0)
            {                                                          
                GetImage(str[0], str[4]);                    
            }
            count++;
        }
        //GetImage(@"http://www.example.com/picture.jpg", "1");
    }


    private void GetImage(string url, string prodID)
    {   
        string saveLocation = textBox2.Text + @"\";;
        saveLocation += prodID + ".jpg";                        
        WebClient webClt = new WebClient();
        webClt.DownloadFile(url, saveLocation);                         
    }

你的CSV文件中是否可能包含双引号?那么你应该将它们替换掉... - Kai
我的 CSV 文件中没有它们... - user1744966
也许问题出在 saveLocation 变量上?最好使用 Path.Combine() 来处理路径(比使用 textBox2.Text + @"\" + prodID + ".jpg"; 更好)。你能检查一下 saveLocation 的值吗? - Nasreddine
尝试调用 webClt.DownloadFile(new Uri(url), saveLocation);,似乎部分输入不是有效的 URI。 - Candide
@user1744966,你能否发布一个 List<string[]> file 内容的示例? - Nasreddine
显示剩余2条评论
1个回答

2
无论是哪个函数或方法创建了这些引号,你都可以将它们全部替换。
String myUrl = str[0];
myUrl = myUrl.Replace("\"", "");
GetImage(myUrl, str[4]);

我认为你的文件中包含引号或者parseCSV方法会创建它们。

更新:

我使用了这段代码,它可以完全正常地工作且不需要使用引号:

static void Main(string[] args)
{
  string fileName = "Test";
  //folderBrowserDialog1.ShowDialog();
  string saveLocation = ".\\";
  //textBox2.Text = saveLocation;
  List<string[]> file = new List<string[]>
  {
    new string[] { "http://www.example.com", "1", "1", "1", "1"},
    new string[] { "http://www.example.com", "2", "2", "2", "2"},
  };
  int count = 0;
  foreach (string[] str in file)
  {
    if (count != 0)
    {
        GetImage(str[0], str[4]);
    }
    count++;
  }
//GetImage(@"http://www.example.com/picture.jpg", "1");
}


private static void GetImage(string url, string prodID)
{
  string saveLocation = ".\\"; // textBox2.Text + @"\"; ;
  saveLocation += prodID + ".jpg";
  WebClient webClt = new WebClient();
  Console.WriteLine(url);
  webClt.DownloadFile(url, saveLocation);
}

尝试过了,没有任何区别。我的 CSV 文件没有引号(除非它们以某种方式隐藏)。而且我非常确定它们没有被放在 parseCSV 中。using (StreamReader readFile = new StreamReader(pth)) { string line; string[] row; while ((line = readFile.ReadLine()) != null) { row = line.Split(','); parsedData.Add(row); } } - user1744966
它能够工作是因为你明确地定义了string[]。我的问题是当它从parseCSV获取字符串时。我试图在之前的评论中发布那段代码,但它看起来有点丑陋。我不知道引号来自哪里,以及为什么字符串替换无法将它们删除。 - user1744966
1
好的,由于某些原因,当我尝试按照你最初建议的方式在发送之前替换它时,它没有起作用。但是我发现这个代码:GetImage(str[0].Replace("\"", ""), str[4]);已经解决了问题。感谢你的帮助。 - user1744966

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接