I am trying to save a file in a database of SQL SERVER 2008 and I am not managing to send the data to the server side or I am not receiving it well not what I know.
Front-End
@model PCotiza_compras.Models.testsoru
@{
ViewBag.Title = "Kyo_2";
}
<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'></script>
<div class="container">
<div class=row"">
<div class="col-xs-12">
<script>
var f = new Date();
document.write(f.getDate() + "/" + (f.getMonth() +1) + "/" + f.getFullYear());
</script>
</div>
<div class="col-lg-6">
@using (Html.BeginForm("KyoTest", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) {
<table class="table table-hover">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
</thead>
<tbody>
<tr>
<td>Nombre</td>
<td>
<input type="text" placeholder="NombreDeProyecto" id="name" />
</td>
</tr>
<tr>
<td>Formato</td>
<td>
<label>Formato: </label><input type="file" id="format" name="files[]" multiple />
<output id="list"></output>
<script>
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// files is a FileList of File objects. List some properties.
var output = [];
for (var i = 0, f; f = files[i]; i++) {
output.push('<h4>Datos de archivo</h4>', '<label>Nombre: </label><li>', escape(f.name),
'<input class="hidden" type="text" id="fn" name="name" value="', escape(f.name), '" /></li>',
'<label>Tipo: </label><li>', f.type || 'n/a',
'<input class="hidden" type="text" id="ft" name="name" value="', f.type, '" /></li>',
'<label>Bytes: </label><li> ', f.size,
'<input class="hidden" type="text" id="fs" name="name" value="', f.size, '" /></li>',
' <label>Modificado: </label><li> ',
f.lastModifiedDate.toLocaleDateString(), '</li>');
}
document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
}
document.getElementById('format').addEventListener('change', handleFileSelect, false);
</script>
</td>
</tr>
<tr>
<td>Wiw</td>
<td>
<p>
@Session["Wiw"]
<input type="text" class="hidden" id="wiw" value="@Session["WiW"]" />
</p>
</td>
</tr>
<tr>
<td>
<input type="button" onclick="trymeplz()" name="name" value="trymeplz" />
<input type="submit" value="Upload Image" />
</td>
</tr>
</tbody>
</table>
}
</div>
<div class="col-lg-6">
<output id="list"></output>
</div>
</div>
</div>
<script>
function trymeplz(){
var obj = {};
var dt = new Date();
obj.namePro = $("[id*=name]").val();
obj.format = $('#format').prop("files")[0];
obj.wiw = document.getElementById("wiw").value;
obj.fname = document.getElementById("fn").value;
obj.sz = document.getElementById("fs").value;
obj.ftype = document.getElementById("ft").value;
obj.dt = dt.getDate() + "/" + (dt.getMonth() + 1) + "/" + dt.getFullYear();
var jsonob = JSON.stringify(obj);
alert(jsonob);
$.ajax({
url: '@Url.Action("KyoTest", "Home")',
method: "POST",
dataType: 'json',
data: jsonob,
processData: false,
contentType: 'multipart/form-data',
success: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
},
error: function(er){
alert("something got wrong" + er);
}
});
}
</script>
Back-End / Server side
[HttpPost]
public ActionResult KyoTest(testsoru form) {
string constr = "Data Source=DMX87025;Initial Catalog=DB_PCC;Integrated Security=True";
using(SqlConnection con = new SqlConnection(constr)) {
string query = "INSERT INTO Testsoru([Name],[Wiw],[Formato],[Size],[FName],[Type])" +
" VALUES(@Name,@wiw,@format,@Size,@FName,@Type)";
using(SqlCommand cmd = new SqlCommand(query)) {
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", form.name);
cmd.Parameters.AddWithValue("@wiw", form.wiw);
cmd.Parameters.AddWithValue("@format", form.formato);
cmd.Parameters.AddWithValue("@Size", null);
cmd.Parameters.AddWithValue("@FName", null);
cmd.Parameters.AddWithValue("@Type", null);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
con.Close();
}
}
return View(form);
}
//----Modelo de datos
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PCotiza_compras.Models {
public class testsoru {
public int id {
get;
set;
}
public string name {
get;
set;
}
public string wiw {
get;
set;
}
public string size {
get;
set;
}
public string ftype {
get;
set;
}
public string fname {
get;
set;
}
public HttpPostedFileWrapper formato {
get;
set;
}
}
}