0

我在上传文件时遇到问题。我想从我的编辑视图上传它:

    <% 
    using (Html.BeginForm("edit","profile",FormMethod.Post, new { enctype="multipart/form-data" }))
    {%>
    <%: Html.ValidationSummary(true) %>

    <%: ViewData["ErrorMessage"] %>
    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Image) %>
        </div>
        <div class="editor-field">
            <input type="file" id="Image" name="Image" />
            <label id="LabelErrorImage" class="errorMessage" />
        </div> 

        <p>
            <input type="submit" value="Save" onclick="return Validate(); return false;"/>
        </p>
    </fieldset>

<% } %>

我想使用HttpPostedFileBase类。我的编辑操作:

[Authorize]
        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string id, HttpPostedFileBase file, FormCollection formValues)
        {
                    if (ModelState.IsValid)
                    {

                        if (file != null && file.ContentLength > 0)
                        {
                            CustomHelpers.createFolder();
                            var tmpPath = MyConfig.UPLOAD_FILE_PATH + "/" + Membership.GetUser().ProviderUserKey.ToString();
                            var path = Path.Combine(Server.MapPath(MyConfig.UPLOAD_FILE_PATH), "Avatar");
                            var fileExtension = Path.GetExtension(file.FileName);
                            file.SaveAs(path);
                            user.Image = "Avatar";
                        }
                        adventureDB.SaveChanges();

                        return RedirectToAction("Index");
                    }
        }

但是我总是有空文件对象,为什么??????你有什么想法,建议为什么它可以这样工作?也许我如何将文件值传递给我的编辑操作有问题?


编辑: 即使我删除它也很奇怪
using (Html.BeginForm("Index","Profile",FormMethod.Get, new { enctype="multipart/form-data" }))

页面源仍然有:

<body>

    <form method="post" action="6111e591-b92d-4bcb-b214-ab8f664b35f9" id="form1">

我的意思是我不能更改标签但不知道为什么:/

4

3 回答 3

1

The solution of this problem when:

  1. We use Master.Site,
  2. We want to upload file in a view,
  3. We are sure that it should work but we all the time has null,

Then:

  1. Guys were right - I had wrong name in my view - check it!
  2. Check source code of your view and if you have 2 < form > tags you should remove the < form > tag from Master site as then the second one is ignored!

Now it should work.

于 2011-06-09T09:23:37.480 回答
1

尝试改变: -

public ActionResult Edit(string id, HttpPostedFileBase file, 
  FormCollection formValues)

至:-

public ActionResult Edit(string id, HttpPostedFileBase image, 
  FormCollection formValues)

因为您输入的名称是image

 <input type="file" id="Image" name="Image" />

编辑 老实说,其他东西正在停止图像的绑定。这是您发布的整个表格吗?

需要测试的几件事

  1. 你有两次 HTTPOST 装饰你的方法,虽然我不相信这应该有所作为。
  2. 查看源代码并确保源代码中没有其他名称name=image
  3. 在再次测试之前,请确保清空缓存并确保来源正确
  4. 尝试使用<form action="/profile/index" method="post" enctype="multipart/form-data">
  5. 从您上次的编辑来看,您的母版页/布局有问题吗?这是一个 mvc/webforms 混合体吗?
于 2011-06-08T16:37:29.770 回答
0

好吧,在您看来,您将文件输入命名为“图像”,但您的操作方法接受一个名为“文件”的参数。重命名其中一个,它应该可以工作。

于 2011-06-08T16:37:46.567 回答