2015-05-06 17 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