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