我正在读取一个文本文件,其中包含在我正在处理的 MVC 网站中使用 C# 的 SQL 插入语句。在调试我正在使用的函数时,它工作正常并且发生了插入。但是,一旦我发布该站点并在我的本地计算机上运行它(IIS 设置为甚至使用 asp.net 4.0),它似乎就不起作用了。
if (Request.Files != null && Request.Files["txtImportFile"] != null)
{
//newFilePath = Server.MapPath("\\" + DateTime.Now.Ticks + Request.Files["txtImportFile"].FileName);
string[] temp_string = Request.Files["txtImportFile"].FileName.Split(new char[] { '\\' });
string temp_filename = temp_string[temp_string.Count() - 1];
//newFilePath = Server.MapPath("\\temp\\" + DateTime.Now.Ticks + Request.Files["txtImportFile"].FileName);
newFilePath = Server.MapPath("\\temp\\" + DateTime.Now.Ticks + temp_filename);
Request.Files["txtImportFile"].SaveAs(newFilePath);
StreamReader reader = new StreamReader(newFilePath);
string contents = reader.ReadToEnd();
reader.Close();
Models.WingsRemoteDbLibrary dbLib = new Models.WingsRemoteDbLibrary();
string update_message = dbLib.UpdateSlaveItemsTable(contents);
if (System.IO.File.Exists(newFilePath))
System.IO.File.Delete(newFilePath);
RandomPopupView(update_message);
}
我希望我的解释听起来没有含糊不清。我会尽力回答任何进一步的问题。谢谢。
最佳答案
解决方法:
而不是使用
Server.MapPath("\\temp\\"...
在根目录下创建名为“temp”的文件夹并使用
System.Web.HttpContext.Current.Request.MapPath("~\\temp....
关于c# - ASP.NET MVC网站从磁盘读取文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5699079/