Estoy desarrollando esta parte de una página en donde necesito guardar un archivo (.xls ,.docx ,.png, etc) en una base de SQl Server, tambien necesito obtener las propiedades de este archivo como su nombre, tipo, tamaño; creo que ya casi lo tengo, pero sigo obteniendo este error por parte del servidor, al parecer todo lo que mando es Null. Estoy utilizando Ajax / js para pasar mi información al lado del servidor , estoy usando un alert solo para saber si estoy jalando bien la información. Pero mi error aparece en el lado del servidor.
(Nota: Wiw es mi username de mi página)
Codigo del front end (Creo que aquí no hay ningun problema)
< script >
function trymeplz() {
var object = {};
object.namePro = $("[id*=name]").val();
object.file = $('#format').prop("files")[0];
object.wiw = document.getElementById("wiw").value;
alert("{'namePro' : '" + object.namePro + "'," +
"'file' : '" + object.file + "' ," +
"'wiw' : '" + object.wiw + "'" +
"}");
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: '@Url.Action("KyoTest", "Home")',
data: "{'namePro' : '" + object.namePro + "'," +
"'file' : '" + object.file + "' ," +
"'wiw' : '" + object.wiw + "'" +
"}",
success: function(response) {
alert("at least Ajax looks fine" + response);
},
Error: function(response) {
alert("something got wrong" + response);
}
});
} <
/script>
@model PCotiza_compras.Models.testsoru @{ ViewBag.Title = "Kyo_2"; }
<div class="container">
<div class=row "">
<div class="col-lg-6">
<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" />
</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" />
</td>
</tr>
</tbody>
</table>
</div>
<div class="col-lg-6"></div>
</div>
</div>
Codigo del back end (View test es una vista no se preocupen por ella)
[HttpPost]
public ActionResult KyoTest(testsoru test, HttpPostedFileBase[] files) {
string constr = "Data Source=DMX87025;Initial Catalog=DB_PCC;Integrated Security=True";
foreach(HttpPostedFileBase file in files) {
int size = file.ContentLength;
string fname = file.FileName;
int position = fname.LastIndexOf("\\");
fname = fname.Substring(position + 1);
string contentType = file.ContentType;
byte[] fileData = new byte[size];
file.InputStream.Read(fileData, 0, size);
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", test.name);
cmd.Parameters.AddWithValue("@wiw", test.wiw);
cmd.Parameters.AddWithValue("@format", test.formato);
cmd.Parameters.AddWithValue("@Size", size);
cmd.Parameters.AddWithValue("@FName", fname);
cmd.Parameters.AddWithValue("@Type", contentType);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
con.Close();
}
}
}
return View(test);
}
Mi error aparece justo por aquí en el lado del servidor, tambien les dejo una vista del diseño de mi tabla y de mi modelo de datos para un mejor entendimiento.
Modelo de datos
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PCotiza_compras.Models {
public class testsoru {
internal HttpFileCollection Files;
internal HttpPostedFileWrapper file;
public int id {
get;
set;
}
public string name {
get;
set;
}
public string wiw {
get;
set;
}
public HttpPostedFileWrapper formato {
get;
set;
}
}
}
Muchas grácias a toda la comunidad, son geniales :)