ASP.NET MVC,檔案的兩種上傳方式

來源:互聯網
上載者:User

標籤: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,檔案的兩種上傳方式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.