string line;
string path = UploadContacts.PostedFile.FileName;
var reader = new StreamReader(path);
while ((line = reader.ReadLine()) != null)
{
var link = line;
if (link != string.Empty)
{
// another functionality ;
}
Console.WriteLine(line);
}
这里UploadContacts
是文件上传控件。我正在通过这个上传一个文档文件。我的目标是逐行阅读word文档的每一行并进入另一个功能,但它不起作用。
引发异常Could not find file 'C:Program Files (x86)Common FilesMicrosoft SharedDevServer10.0tr.docx'.
有什么建议吗?
逐行读取,请使用以下代码:
// Loop through all lines in the document.
Application application = new Application();
Document document = application.Documents.Open("C:\new folder\word.docx");
String read = string.Empty;
List<string> data = new List<string>();
for (int i = 0; i < document.Paragraphs.Count; i++)
{
string temp = document.Paragraphs[i + 1].Range.Text.Trim();
if (temp != string.Empty)
data.Add(temp);
}
// Close word.
application.Quit();`
文件是在用户计算机上还是托管 Web 应用程序的服务器上?
由于您说用户正在上传文件,因此我的印象是用户正在选择其计算机上的路径。 请记住,您的 c# 代码正在其他位置的服务器上执行。 它无法直接访问用户计算机(除非它位于共享网络上)。
过去,当我需要让用户将文件上传到我的 Web 应用程序时,我会使用 plupload jquery 控件。 与 mvc 一起使用非常简单。 这是其他人的堆栈溢出问题,它将帮助您弄清楚如何使用它......将 plupload 与 MVC3 结合使用