我正面临一个可能无法解决的非常明显的问题。我正在将数据从CSV文件导入到我的SQL DB-在我的开发环境中这是完美的方法,但是一旦部署到我们的专用服务器上将无法正常工作(这是临时测试,将移至只有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'
现在看起来似乎很明显(找不到文件),我正在使用一个对话框,在其中选择我的PC上的文件(其他文件将在他们的PC上执行),所以我不需要保存文件(除非我必须保存,如果生病甚至拥有权限)。
我尝试使用,FileUpload.SaveAs(FileUpload.FileName)
但随后出现一个错误,该另存为只允许保存在根目录中。然后我尝试了一下FileUpload.SaveAs("~/" + FileUpload.FileName)
,这说我无权访问该文件夹...
我认为将从指定的路径读取文件,而不是从部署Web应用程序的服务器上的路径读取文件?
我已经为此苦苦挣扎了一段时间-有没有人知道的解决方法?
提前致谢!
当你跑步
System.IO.File.ReadAllLines(FileUpload.FileName)
您是在告诉.net从文件系统中读取一个文件名,该文件名与上载的文件名相同,而不是用户上载的文件。
如果要在本地保存文件,请在使用SaveAs方法之前尝试将路径映射到要保存到的位置(可能是App_Data):
public ActionResult Index(HttpPostedFileBase file) {
if (file.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
//read your file's contents here
}
return RedirectToAction("Index");
}
取自http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句