从磁盘问题ASP.NET MVC网站读取文件 [英] ASP.NET MVC Website Read File from Disk Problem

查看:193
本文介绍了从磁盘问题ASP.NET MVC网站读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读包含使用C#在MVC网站,我正在为SQL INSERT语句中的文本文件。当调试功能,我使用的作品很好,插入发生。但是,一旦我发布网站和我的本地机器上运行(与IIS设置以asp.net 4.0使用连)它似乎并没有工作。

I'm reading a text file containing an insert statement for SQL using C# in an MVC Website I'm working on. When debugging the function I'm using works fine and the insert occurs. But once I publish the site and run it on my local machine (with IIS set-up to use asp.net 4.0 even) it doesn't seem to work.

        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);
        }

我希望我的解释听起来并不含糊。我会尽我所能回答任何进一步的问题。谢谢你。

I hope my explanation doesn't sound vague. I'll try my best to answer any further questions. Thanks.

推荐答案

解决方法:

而不是使用

Server.MapPath("\\temp\\"...

在根目录下创建文件夹的名称为TEMP和使用

Create folder under root with name "temp" and use

System.Web.HttpContext.Current.Request.MapPath("~\\temp....

这篇关于从磁盘问题ASP.NET MVC网站读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆