View & JS:
@model RCRS.WebApp.LG.Calender.Web.Models.FileModel

@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, new { id = "uploadForm", enctype = "multipart/form-data" }))
{
    @Html.TextBox("data", "", new { type = "file", size = 40, required = "required" })
    @Html.HiddenFor(todocd=>Model.todocd)
    @Html.HiddenFor(todoGroupcd => Model.todoGroupcd)
    <input type="submit" id="submit_upload" value="アップロード" />
}

<div id="todo-file-list">
    @Html.Partial("_todofilelist")
</div>
 
<script src="@Url.Content("~/Scripts/jquery.form.js")" type="text/javascript"></script>
<script type="text/javascript">
    (
function () {
        
var todoList = $('#todo-file-list');
        
var filePath = $('input[name="data"]');
        $('#uploadForm').on('submit', 
function (e) {
            e.preventDefault();
            $(
this).ajaxSubmit({
                complete: 
function (xhr) {
                    filePath.val('');
                    todoList.html(xhr.responseText);
                }
            });
        });
    })();
</script>

Controller
        //-----------------------------------------------------------------------
        /// <summary>
        
/// FileUploadリクエスト時に発生します。
        
/// </summary>
        
/// <param name="file"></param>
        
/// <returns></returns>
        //-----------------------------------------------------------------------
        [HttpPost]
        public ActionResult FileUpload(FileModel file)
        {
            int    retValue     = 0;
            string msg          = string.Empty;
            string fileName     = string.Empty;
            int    fileSize     = 0;
            int    todoGroupcd  = file.todoGroupcd;
            int    todocd       = file.todocd;
            string div          = Settings.Default.IsRCRS;
            DateTime createDate = DateTime.Now;

            HttpPostedFileBase data = Request.Files["data"];
            fileName = data.FileName;
            fileSize = (data.ContentLength / 1000);


            using (CalenderServiceClient svc = new CalenderServiceClient())     //Calender Serviceを経由
            {
                try
                {
                    retValue = svc.FileUpload(todoGroupcd, todocd, div, fileName, fileSize.ToString(), createDate);       //Uploadファイルをbyte配列に書き込む

                    
//戻り値でメッセージを分岐
                    if (retValue == 0)
                    {
                        msg             = CommonFunctions.GetFileName(fileName) + " をアップロードしました。";
                        ViewBag.Message = msg;
                    }
                    else if (retValue == -1)
                    {
                        msg             = "ファイルのアップロードに失敗しました。";
                        ViewBag.Message = msg;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    //サービス接続を終了
                    svc.Close();
                }
            }

            TodoModel todoModel = new TodoModel();
            FileModel files = todoModel.GetBizFile(todocd, todoGroupcd);

            return Content(CommonFunc.RenderViewToString(ControllerContext, "_todofilelist", files));
        }
posted on 2017-02-27 16:50 Ying-er 阅读(217) 评论(0)  编辑  收藏 所属分类: .Net

只有注册用户登录后才能发表评论。


网站导航: