无法在ASP.NET上找到csv文件

时间:2011-11-25 09:50:50

标签: c# asp.net deployment

任何知道如何解决此错误的人都可以帮助我。下面是我读取csv文件的代码。当我尝试上传时,在'/'应用程序中显示* 服务器错误。找不到文件'C:/ ... csv * 我是c#的初学者。

ReadCSV

    string filename = FileUpload1.PostedFile.FileName;

    using (CsvFileReader reader = new CsvFileReader(filename))
    {
        CsvRow row = new CsvRow();
        while (reader.ReadRow(row))
        {
            foreach (string s in row)
            {
                Console.Write(s);
                Console.Write(" ");
                TextBox1.Text += s;
            }
            Console.WriteLine();
        }
    }

CSVClass

  public class CsvFileReader : StreamReader
    {
        public CsvFileReader(Stream stream)
            : base(stream)
        {
        }

        public CsvFileReader(string filename): base(filename)
        {
        }

        /// <summary>
        /// Reads a row of data from a CSV file
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public bool ReadRow(CsvRow row)
        {
            row.LineText = ReadLine();
            if (String.IsNullOrEmpty(row.LineText))
                return false;

            int pos = 0;
            int rows = 0;

            while (pos < row.LineText.Length)
            {
                string value;

                // Special handling for quoted field
                if (row.LineText[pos] == '"')
                {
                    // Skip initial quote
                    pos++;

                    // Parse quoted value
                    int start = pos;
                    while (pos < row.LineText.Length)
                    {
                        // Test for quote character
                        if (row.LineText[pos] == '"')
                        {
                            // Found one
                            pos++;

                            // If two quotes together, keep one
                            // Otherwise, indicates end of value
                            if (pos >= row.LineText.Length || row.LineText[pos] != '"')
                            {
                                pos--;
                                break;
                            }
                        }
                        pos++;
                    }
                    value = row.LineText.Substring(start, pos - start);
                    value = value.Replace("\"\"", "\"");
                }
                else
                {
                    // Parse unquoted value
                    int start = pos;
                    while (pos < row.LineText.Length && row.LineText[pos] != ',')
                        pos++;
                    value = row.LineText.Substring(start, pos - start);
                }

                // Add field to list
                if (rows < row.Count)
                    row[rows] = value;
                else
                    row.Add(value);
                rows++;

                // Eat up to and including next comma
                while (pos < row.LineText.Length && row.LineText[pos] != ',')
                    pos++;
                if (pos < row.LineText.Length)
                    pos++;
            }
            // Delete any unused items
            while (row.Count > rows)
                row.RemoveAt(rows);

            // Return true if any columns read
            return (row.Count > 0);
        }
    }

2 个答案:

答案 0 :(得分:1)

FileUpload1.PostedFile.FileName是客户端/浏览器中的文件名 - 它不包含路径...

您可以使用FileUpload1.PostedFile.InputStream来访问它

using (CsvFileReader reader = new CsvFileReader(FileUpload1.PostedFile.InputStream))

或者您首先通过FileUpload1.PostedFile.SaveAs将其保存到磁盘(您需要权限的任何位置),然后访问该文件。

答案 1 :(得分:0)

您需要先将要上传的文件保存到磁盘。像这样:

string fileSavePath= Sever.MapPath("/files/" + FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(fileSavePath);

using (CsvFileReader reader = new CsvFileReader(fileSavePath))
....

尚未测试此代码,但应该为您提供一个起点。