I am developing this part of a page where I need to save a file (.xls, .docx, .png, etc) on a SQl Server basis, too I need to get the properties of this file as its name, type, size; I think I almost have it, but I still get this error from the server, apparently everything I send is Null . I am using Ajax / js to pass my information to the server side, I am using a alert just to know if I am pulling the information well. But my error appears on the server side.
( Note: Wiw is my username on my page)
Front end code (I think there's no problem here)
< 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>
Back end code (View test is a view do not worry about it)
[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);
}
My error appears right here on the server side, I also give you a view of the design of my table and my data model for a better understanding.
Data model
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;
}
}
}
Many thanks to the whole community, they are great :)