0

On the local machine the code below works fine, but when hosted on remote server it doesn't seem to find the image file. I created the Image folder and gave it read/ write permissions as well

// --------------------------------------------
Error

Server Error in '/' Application.  
Could not find file 'IdImage.jpg'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException: Could not find file 'IdImage.jpg'.

Code is as below
// ---------------------------------------------

[HttpPost]  
public ActionResult Index(HttpPostedFileBase file)  
{  
    if (file != null && file.ContentLength > 0)   
    {  
       var fileName = Path.GetFileName(file.FileName);  
       var path = Path.Combine(Server.MapPath("~/Images/Photo"), fileName);  
       file.SaveAs(path);  
    }  

    return RedirectToAction("Index");          
}
4

2 回答 2

0

这看起来很像权限问题。您说您已经创建了该Images文件夹并授予了它读/写权限,但是从我在您的代码中看到的内容来看,您正试图将图像存储在一个名为Photo. 因此,请确保您也已创建此文件夹并为其授予读/写权限。还要确保将这些权限授予正确的帐户 - 在 IIS 中配置为运行托管应用程序的应用程序池的帐户。

于 2013-01-13T09:36:12.610 回答
0

我使用此代码

[HttpPost]
public ActionResult Upload(System.Web.HttpPostedFileBase file)
{
       string filename = Server.MapPath("~/files/somename");
       file.SaveAs(filename);
       return RedirectToAction("Index");
}

在视野中

@{
    ViewBag.Title = "Upload";
}
<h2>
    Upload</h2>
@using (Html.BeginForm(actionName: "Upload", controllerName: "User", 
                       method: FormMethod.Post,
                       htmlAttributes: new { enctype = "multipart/form-data" }))
{
    <text>Upload a photo:</text> <input type="file" name="photo" />
    <input type="submit" value="Upload" />
}
于 2013-01-14T15:51:38.387 回答