我坐在一起可能是一个非常明显的问题,我无法找到解决方案。我正在将数据从CSV文件导入到我的SQL数据库 - 这在我的开发环境中工作得很好,但是一旦部署到我们的专用服务器就不会工作(这是临时的测试,它将被移动到我只有ftp的Web服务器访问,所以无法安装任何东西)。
这是我的代码:
public ActionResult ImportExcel(PipelineDetails model, HttpPostedFileBase FileUpload, int ProjectID)
{
string Result = "";
string UploadfileSaved_Path;
ProjectManager PM = new ProjectManager();
PipelineData PD = new PipelineData();
try
{
if (!FileUpload.FileName.EndsWith("csv"))
{
Result = "File type not allowed. Please import file using CSV format(Comma Delimited).";
return RedirectToAction("Index", "Pipeline", new { id = ProjectID, result = Result });
}
string[] lines = System.IO.File.ReadAllLines(FileUpload.FileName);
bool isHeader = true;
int i = 0;
foreach (string line in lines)
{
string[] col = line.Split(new char[] { ',' });
if (i >= 1)
{
isHeader = false;
}
else
{
if (col[0] != "Accumulated_Length" || col[1] != "Elevation" || col[2] != "Pipe_Outside_Diameter" ||
col[3] != "Wall_Thickness")
{
Result = "Import files Headers are incorrect. Please correct before retrying.";
return RedirectToAction("Index", "Pipeline", new { id = ProjectID, result = Result });
}
}
if (col[0] == null || col[1] == null || col[2] == null || col[3] == null)
{
Result = "Some values are missing in the Import File. Please check the data.";
return RedirectToAction("Index", "Pipeline", new { id = ProjectID, result = Result });
}
if (!isHeader)
{
if (i == 1)
{
PM.DeleteALLPipeLine_forProject(ProjectID);
}
using (RexusTradingEntities RTE = new RexusTradingEntities())
{
PD.Accumulated_Length = Convert.ToDecimal(col[0]);
PD.Elevation = Convert.ToDecimal(col[1]);
PD.Pipe_Outside_Diameter = Convert.ToDecimal(col[2]);
PD.Wall_Thickness = Convert.ToDecimal(col[3]);
if (col[4] != "" && col[4] != null)
{
PD.Control_Point_Description = col[4];
}
if (col[5] != "" && col[5] != null)
{
PD.Control_Point_Size = Convert.ToDecimal(col[5]);
}
PD.fkiProjectID = ProjectID;
PD.CreateDateTimeStamp = DateTime.Now;
RTE.PipelineDatas.Add(PD);
RTE.SaveChanges();
}
}
i++;
}
}
catch (Exception ex)
{
//Result = "There is a problem with the Import File. Please check the file format or data.";
Result = ex.Message + " : " + FileUpload.FileName;
return RedirectToAction("Index", "Pipeline", new { id = ProjectID, result = Result });
}
//Adding the Node Numbers in sequencial order
PM.UpdatePipelineNodeNumbers(ProjectID, model);
Result = "Import Success";
return RedirectToAction("Index", "Pipeline", new { id = ProjectID, result = Result });
}
我得到的错误是:'Could not find file 'C:\Windows\SysWOW64\inetsrv\RexusImportTemplate.csv'
现在看起来很明显(文件未找到),我正在使用一个对话框,我在我的电脑上选择文件(其他人会在他们的电脑上),所以我不需要保存文件(除非我必须,如果生病甚至有权限。)
我尝试使用FileUpload.SaveAs(FileUpload.FileName)
,但后来我得到一个错误,这个saveas只允许保存在根目录中。然后我尝试了FileUpload.SaveAs("~/" + FileUpload.FileName)
,这说我无法访问该文件夹...
我认为该文件将从指定的路径中读取,而不是部署Web应用程序的服务器上的路径...?
我现在已经在这方面挣扎了一段时间 - 有没有人知道的任何工作?
提前致谢!
答案 0 :(得分:1)
我认为您需要在服务器上映射路径。 Server.MapPath(PathWhereFilesWillBeSaved)
能够将文件保存在服务器上的项目目录中。 MSDN - Server.MapPath
答案 1 :(得分:1)
两件事:
答案 2 :(得分:0)
运行时
node {
stage 'Build A'
build 'A'
}
你告诉.net从文件系统读取一个文件,其文件名与上传的文件相同,而不是用户上传的文件。
如果要在本地保存文件,请尝试在使用SaveAs方法之前将路径映射到要将其保存到的位置(可能是App_Data):
System.IO.File.ReadAllLines(FileUpload.FileName)
取自http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/