码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET MVC,文件的两种上传方式

时间:2018-08-24 10:44:52      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:ppa   load   RoCE   ror   str   cti   radio   question   mp3   

文件类型可以更换,在后台保存的时候更换下保存格式
@*视图*@
<div id="checkQuestionnaireAdd">
    <form id="editform">
        <input id="id" type="hidden" name="id" value="0" />
        <table class="t1" style="width:100%">
            <tr>
                <td class="ltd" width="100px"><input id="code" class="l-text" name="code" type="hidden" />测试1:</td>
                <td class="rtd"><input id="answer_One" class="l-text" name="answer_One" style="width:270px" type="text" /></td>
            </tr>
            <tr>
                <td class="ltd" width="100px"><input id="code" class="l-text" name="code" type="hidden" />测试2:</td>
                <td class="rtd" id="answer_Two">
                    <input id="radio1" type="radio" value="1" name="gender" />A
                    <input id="radio2" type="radio" value="2" name="gender" />B
                </td>
            </tr>
            <tr>
                <td class="ltd" width="100px"><input id="code" class="l-text" name="code" type="hidden" />测试3:</td>
                <td class="rtd" id="answer_Three">
                    <input id="checkbox1" type="checkbox" name="vehicle" value="1" />A
                    <input id="checkbox2" type="checkbox" name="vehicle" value="2" />B
                    <input id="checkbox3" type="checkbox" name="vehicle" value="3" />C
                    <input id="checkbox4" type="checkbox" name="vehicle" value="4" />D
                </td>
            </tr>
            <tr>
                <td class="ltd" width="100px">录音上传:</td>
                <td>
                    <input type="file" name="file" id="file" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <div align="center">
                        <input type="submit" id="submitid" value="提交" class="l-button" onclick="sub()" />
                    </div>
                </td>
            </tr>
        </table>
    </form>
</div>
 
<script src="/Scripts/jquery-1.12.4.js"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
 @*使用ajaxFileUpload提交*@
<script src="/Scripts/ajaxfileupload.js"></script>
<script type="text/javascript">
    function sub() {
        var answer_Two;
        var radio = document.getElementsByName("gender");
        for (i = 0; i < radio.length; i++) {
            if (radio[i].checked) {
                answer_Two = radio[i].value;
            }
        }
        var answer_Three = "";
        var checkbox = document.getElementsByName("vehicle");
        for (var i = 0; i < checkbox.length; i++) {
            if (checkbox[i].checked) {
                answer_Three += checkbox[i].value;
            }
        }
    $.ajaxFileUpload({
     url: "@Url.Action("questionnaireAddData")",
     type: "post",
     dataType: "json",
     data: {
         answer_One: $("#answer_One;").val(),
         answer_Two: answer_Two,
         answer_Three: answer_Three
     },
     fileElementId: "file", // 上传文件的id、name属性名
     success: function (data) {
         $.ligerDialog.alert(data.Message, function () { window.parent.init(); });
       },
        error: function (e) {
         $.ligerDialog.warn(e.responseText);
       }
    });
}
</script>
@*使用form表单和ajax提交*@
<script type="text/javascript">
    function sub() {
        var form = document.getElementById(‘editform‘),
            formData = new FormData(form);
        var answer_Two;
        var radio = document.getElementsByName("gender");
        for (i = 0; i < radio.length; i++) {
            if (radio[i].checked) {
                answer_Two = radio[i].value;
            }
        }
        var answer_Three = "";
        var checkbox = document.getElementsByName("vehicle");
        for (var i = 0; i < checkbox.length; i++) {
            if (checkbox[i].checked) {
                answer_Three += checkbox[i].value;
            }
        }
        formData.append("answer_Two", answer_Two);
        formData.append("answer_Three", answer_Three);
        $.ajax({
            url: "questionnaireAddData",
            type: "post",
            data: formData,
            processData: false,
            contentType: false,
            success: function (res) {
                if (res) {
                    alert("上传成功!");
                }
                console.log(res);
            },
            error: function (err) {
                alert("网络连接失败,稍后重试", err);
            }
        });
    }
</script>
@*控制器*@
public JsonResult questionnaireAddData(Model model)
{
    object returnValue = null;
    string address = string.Empty;
    if (Request.Files.Count != 0)
    {
        string path = string.Empty;
        try
        {
            path = Server.MapPath("~/music/");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string specificDate = DateTime.Now.ToString("yyyyMMdd").ToString();
            path += specificDate + "/";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            address = DateTime.Now.ToString("HHmmss") + "-" + Guid.NewGuid().ToString() + ".mp3";
            path += address;
            Request.Files[0].SaveAs(path);
            address = "/music/" + specificDate + "/" + address;
        }
        catch
        {
            returnValue = new { State = 0, Message = "录音保存失败!" };
        }
    }
    return Json(returnValue);
}

ASP.NET MVC,文件的两种上传方式

标签:ppa   load   RoCE   ror   str   cti   radio   question   mp3   

原文地址:https://www.cnblogs.com/zcr1829991218/p/ElevenNights_ASPNETMVC_FileUpload.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!