2015-05-06 16 views
0

在我的应用程序中最近发生的更改之后,仍然在使用数据库中的相对路径显示图像时出现此问题。 Error: 404 NOT FOUND http://localhost:1256/Empdet/%22/Photos/jobs.jpg%22从数据库中显示错误的图像

Screenshot

Controller.js:

$scope.UploadFile = function() { 
    console.log('UploadFile'); 
    console.log($scope.Empdet.PhotoFile); 
    var file = $scope.Empdet.PhotoFile; 
    console.log('file is ' + JSON.stringify(file)); 
    var uploadUrl = "../Photos"; 
    console.log('before file upload'); 
    EmployeeFactory.UploadFile(file, uploadUrl).success(function (response) { 
     $scope.Empdet.PhotoText = response; 
     console.log('$scope.Empdet.PhotoText'); 
     console.log(response); 
    }).error(function() { 
     console.log('error'); 
    }); 
    console.log('after file upload'); 
}; 

service.js:

service.UploadFile = function (file, uploadUrl) { 
    var fd = new FormData(); 
    fd.append('file', file); 
    return $http.post('/Empdet/UploadFile', fd, { 
     transformRequest: angular.identity, 
     headers: { 'Content-Type': undefined } 
    }); 
} 

EmpdetController.cs:

[HttpPost] 
    public ActionResult UploadFile() 
    { 
     var file = Request.Files[0]; 
     var path = Path.Combine(Server.MapPath("~/Photos/"), file.FileName); 
     file.SaveAs(path); 

     // prepare a relative path to be stored in the database and used to display later on. 
     var filename = Url.Content("~/Photos/" + file.FileName); 
     // save to db 
     return Json(filename.ToString(), JsonRequestBehavior.AllowGet); 

    } 
+0

尝试删除第二/从'”〜/ Photos /“'? –

+0

@PrashantGhimire哪个会给你/ PhotosFileName?我不认为这是答案..但我可能是错的 – Billy

+0

@Frankline我认为你的URL中有一些额外的引号(“),浏览器转换为%22,如果这不是你的意图,那就是你的问题。 – Billy

回答

1

从你的函数中取出.toString()FileName属性已经返回一个字符串。

[HttpPost] 
    public ActionResult UploadFile() 
    { 
     var file = Request.Files[0]; 
     var path = Path.Combine(Server.MapPath("~/Photos/") + file.FileName); 
     file.SaveAs(path); 

     // prepare a relative path to be stored in the database and used to display later on. 
     string filename = Url.Content("~/Photos/" + file.FileName); 
     // save to db 
     return Json(filename, JsonRequestBehavior.AllowGet); 

    } 

解析你的控制器的回报。这应该摆脱你的URL中的额外引号(“)

0

controller.js:

$scope.UploadFile = function() { 
    console.log('UploadFile'); 
    console.log($scope.Empdet.PhotoFile); 
    var file = $scope.Empdet.PhotoFile; 
    console.log('file is ' + JSON.stringify(file)); 
    var uploadUrl = '/Empdet/UploadFile'; 
    console.log('before file upload'); 
    EmployeeFactory.UploadFile(file, uploadUrl).success(function (response) { 
     console.log(JSON.parse(response)); 
     console.log('$scope.Empdet.PhotoText'); 
     $scope.Empdet.PhotoText = JSON.parse(response); 

    }).error(function() { 
     console.log('error'); 
    }); 
    console.log('after file upload'); 
}; 

EmpdetController.cs:

[HttpPost] 
    public ActionResult UploadFile() 
    { 
     var file = Request.Files[0]; 
     var path = Path.Combine(Server.MapPath("~/Photos/") + file.FileName); 
     file.SaveAs(path); 

     // prepare a relative path to be stored in the database and used to display later on. 
     string filename = Url.Content("~/Photos/" + file.FileName); 
     // save to db 
     return Json(filename, JsonRequestBehavior.AllowGet); 

    } 

Output

+0

这是应该的如果是前者,只需更新你的问题,如果是后者,请解释你发现了什么问题,以及你的修改如何修正。 – CodeCaster

+0

@ CodeCaster这是确切的答案。上述问题的结果是由我所做的更改,是对代码进行的更改是'return Json(filename.ToString(),JsonRequestBehavior.AllowGet);'改为'return Json(filename,JsonRequestBehavior.AllowGet);'和另一个修改是因为即时获取路径作为字符串而不是JSON.Stringify将其更改为'$ scope.Empdet.PhotoText = JSON.parse(response);'。在此之后,所有问题都已解决,并且应用程序成功运行。 – Frankline