ASP. NET MVC, two ways to upload files

Source: Internet
Author: User

File types can be replaced and saved in the background save format
@* View *@
<div id= "Checkquestionnaireadd" >
<form id= "EditForm" >
<input id= "id" type= "hidden" name= "id" value= "0"/>
<table class= "T1" style= "width:100%" >
<tr>
&LT;TD class= "Ltd" width= "100px" ><input id= "code" class= "L-text" name= "code" type= "hidden"/> Test 1:</td>
&LT;TD class= "RTD" ><input id= "Answer_one" class= "L-text" name= "Answer_one" style= "width:270px" type= "text"/ ></td>
</tr>
<tr>
&LT;TD class= "Ltd" width= "100px" ><input id= "code" class= "L-text" name= "code" type= "hidden"/> Test 2:</td>
&LT;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>
&LT;TD class= "Ltd" width= "100px" ><input id= "code" class= "L-text" name= "code" type= "hidden"/> Test 3:</td>
&LT;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>
&LT;TD class= "Ltd" width= "100px" > Recording upload:</td>
<td>
<input type= "File" name= "file" id= "file"/>
</td>
</tr>
<tr>
&LT;TD colspan= "2" >
<div align= "center" >
<input type= "Submit" id= "Submitid" value= "Submit" 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> @* using Ajaxfileupload submit *@<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"///upload file ID, Name property name
Success:function (data) {
$.ligerdialog.alert (data. Message, function () {window.parent.init ();});
},
Error:function (e) {
$.ligerdialog.warn (E.responsetext);
}
});
}
</script>@* using form forms and Ajax submissions *@
<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 ("Upload successful! ");
}
Console.log (RES);
},
Error:function (Err) {
Alert ("Network connection failed, retry later", err);
}
});
}</script>@* Controller * @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 = "Record save failed!" " };
}
}
Return Json (returnvalue);
}

ASP. NET MVC, two ways to upload files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.