c# - ASP.NET MVC网站从磁盘读取文件问题

标签 c# asp.net-mvc

我正在读取一个文本文件,其中包含在我正在处理的 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/

相关文章:

c# - ASP MVC、延迟加载和 JSON 序列化。如何填写其他属性?

c# - WPF 中 ReactiveUI 的 IViewFor<T> (及其实现类型)的目的是什么?

c# - 从 xml 文件生成 POCO 对象

c# - 如何将 CSV 文件批量插入 SQLite C#

asp.net-mvc - 数据绑定(bind) : 'Models' does not contain a property with the name 'Key'

html - 将 HTML 加载到页面中,首先获取为文本

c# - cshtml 标签中的使用条件

C# 身份验证失败,因为远程方已关闭传输流

c# - 为什么我的 View 没有在 View 模型更改时更新?

c# - 当前对 Controller 类型 {1} 的操作 {0} 的请求不明确