0

我正在将用户的个人资料图片上传到文件夹并将其路径保存到 RavenDB。但是我的代码给了我一个错误,即找不到部分路径。在这条线上

file.SaveAs(path);

代码:

    [HttpPost]
    public ActionResult UploadPic(FileManagement fmanage, HttpPostedFileBase file)
    {


        string email = User.Identity.Name;

        if (file != null && file.ContentLength > 0)
        {
            var FileName = string.Format("{0}.{1}", Guid.NewGuid(), file.ContentType);
            var path = Path.Combine(Server.MapPath("~/App_Dta/Uploads"), FileName);
            file.SaveAs(path);

            using (var session = DocumentStore.OpenSession("RavenMemberShip"))
            {

                var query = from q in Session.Query<Registration>() where q.Email == email select q;
                if (query.Count() > 0)
                {
                    foreach (var updated in query)
                    {
                        fmanage.FileName = FileName;
                        fmanage.Path = path;
                        session.SaveChanges();

                    }
                }
            }
        }
        else ModelState.AddModelError("", "Remove the errors and try again");
        return View();
    }
4

3 回答 3

0

您的路径中有输入错误...

代替...

var path = Path.Combine(Server.MapPath("~/App_Dta/Uploads"), FileName);

和...

var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"), FileName);

您还需要确保您具有写入此目录的相关权限。

根据您的错误,文件路径看起来不正确。

c:\users\wasfa\documents\visual studio 2012\Projects\MvcMembership\MvcMembership\App_Data\Uploads\daed3def-df2b-4406-aa‌​9e-c1995190aa6d.image\jpeg

daed3def -df2b-4406-aa‌​9e-c1995190aa6d.image\jpeg是文件名吗?

尝试:

[HttpPost]
public ActionResult UploadPic(FileManagement fmanage, HttpPostedFileBase file)
{
    string email = User.Identity.Name;

    if (file != null && file.ContentLength > 0)
    {
        var FileName = string.Format("{0}.{1}", Guid.NewGuid(), Path.GetFileName(file.FileName));            
        var path = Path.Combine(Server.MapPath("~/App_Dta/Uploads"), FileName);
        file.SaveAs(path);

        using (var session = DocumentStore.OpenSession("RavenMemberShip"))
        {

            var query = from q in Session.Query<Registration>() where q.Email == email select q;
            if (query.Count() > 0)
            {
                foreach (var updated in query)
                {
                    fmanage.FileName = FileName;
                    fmanage.Path = path;
                    session.SaveChanges();

                }
            }
        }
    }
    else ModelState.AddModelError("", "Remove the errors and try again");
    return View();
}
于 2013-09-27T08:11:28.403 回答
0

之前file.SaveAs(path),尝试检查目录是否存在,如果不存在,则创建一个,

if(CreateFolderIfNeeded(path);
{
    file.SaveAs(path);
}

如果需要,创建目录的私有函数,

private static bool CreateFolderIfNeeded(string path)
        {
            bool result = true;
            if (!Directory.Exists(path))
            {
                try
                {
                    Directory.CreateDirectory(path);
                }
                catch (Exception)
                { result = false; }
            }
            return result;

希望这可以帮助。

于 2013-09-27T08:34:29.143 回答
0

检查var FileName = string.Format("{0}.{1}", Guid.NewGuid(), file.ContentType);代码中的行。

file.ContentType 不会返回您正在上传的文件的扩展名。它应该像daed3def-df2b-4406-aa‌​9e-c1995190aa6d.jpeg而不是daed3def-df2b-4406-aa‌​9e-c1995190aa6d.image\jpeg

使用子字符串从上传的文件中查找扩展名。

希望这有帮助

于 2013-09-27T08:51:18.470 回答