文件上传和持久控件值

时间:2013-09-17 15:20:41

标签: jquery ajax asp.net-mvc

我有一些字段的视图,我想在此视图中上传文件。 当我使用表单提交方法上传它时,我在视图的文本框中输入了数据,我可以从jquery ajax调用上传它,这样我也可以一次更新字段,或者如何在发布后维护控件数据。

模型

public class SView : ViewBase
{
    public string Customer { get; set; }
    public string ProjectNumb { get; set; }
    public string Description { get; set; }
    ....

控制器

    public ActionResult Index()
    {
        return View(ViewBase.Current.Model as SView);
    }

    public ActionResult Upload()
    {
        foreach (string inputTagName in Request.Files)
        {
            HttpPostedFileBase file = Request.Files[inputTagName];
            if (file.ContentLength > 0)
            {
                string filePath = HttpContext.Server.MapPath("~/Images/") + Path.GetFileName(file.FileName);

                file.SaveAs(filePath);
            }
        }

        return RedirectToAction("Index");
    }

查看

<tr class="evendiv">
                    <td>
                        <label class="labelfont">Customer</label>
                    </td>
                    <td>
                        <input id="tbxCust" type="text" value="@Model.Quotation.Customer" />
                    </td>
                </tr>
                <tr class="odddiv">
                    <td>
                        <label class="labelfont">Description</label>
                    </td>
                    <td>
                        <textarea id="tbxDesc" cols="20" rows="5" >@Model.Quotation.Description</textarea>
                    </td>
                </tr>
                <tr class="odddiv">
                    <td>
                        <label class="labelfont">Attach File(s)</label>
                    </td>
                    <td>
                        @using (Html.BeginForm("Upload", "Landing", FormMethod.Post, new { enctype = "multipart/form-data" }))
                        {
                            <input id="attchfile1" type="file" />
                            <input id="Button1" type="submit" value="Upload" />
                        }
                    </td>
                </tr>   

<input id="btnReqHrs" type="button" value="Request Hours" onclick="javascript:requesthours()" />

<script type="text/javascript">
function requesthours() {
    $.ajax({
        type: 'POST',
        url: Requesthours,
        data: values,
        dataType: "json",
        error: function (xhr, ajaxOptions, thrownError) {
            //ErrorMsg(thrownError);
            $("#progressdiv").hide();
        },
        success: function () {
            //$('#innerdiv').html(res);
            $("#progressdiv").hide();
        }
    });
}

2 个答案:

答案 0 :(得分:0)

我想这可能是你的问题,请尝试更改下面的模型,

public class SView : ViewBase
{
    public string Customer { get; set; }
    public string ProjectNumb { get; set; }
    public string Description { get; set; }
    public HttpPostedFileBase file {get; set;}
}

更改Action方法以获取发布值

[httppost]
public ActionResult Upload(SView sview)
    {
        HttpPostedFileBase myFile = Request.Files["MyFile"];
        if (myFile != null && myFile.ContentLength != 0)
        {
            string pathForSaving = Server.MapPath("~/Uploads");
            if (this.CreateFolderIfNeeded(pathForSaving))
              {
                try
                  {
                   myFile.SaveAs(Path.Combine(pathForSaving, myFile.FileName));
                  }
                  catch (Exception ex)
                  {
                    message = string.Format("File upload failed: {0}", ex.Message);
                   }
              }
         }
     }

如果目录不存在,则创建目录的功能

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

答案 1 :(得分:0)

我改变了我的视图按钮,在调用控制器之前,我使用jquery ajax调用来存储我的表单数据。

<input id="Button1" type="submit" value="Upload" onclick="StoreFormData" />