Hola estoy haciendo un módulo en ASP.NET con Razor y C# en donde subo a una base de datos un archivo de cualquier tipo, junto con otros datos. Necesito obtener las propiedades de el archivo del lado del servidor ("con C#"), para poder anexar estos nuevos datos en mi query para tenerlos en la tabla.
Este es el error que me aparece cuando intento obtener los datos.
Este es mi código del lado del cliente y del servidor.
[HttpPost]
public ActionResult KyoTest(testsoru test) {
string constr = "Data Source=DMX87025;Initial Catalog=DB_PCC;Integrated Security=True";
HttpFileCollection files = test.Files;
foreach(string fileTagName in files){
HttpPostedFile file = test.Files[fileTagName];
if (file.ContentLength > 0) {
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)" +
" 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);
}
@using (Html.BeginForm("KyoTest", "Home", FormMethod.Post)) {
<table class="table table-hover" cellpadding="0" cellspacing="0">
<tr>
<th colspan="2" align="center">Customer Details</th>
</tr>
<tr>
<td>Name: </td>
<td>
@Html.TextBoxFor(m => m.name)
</td>
</tr>
<tr>
<td>
@Html.TextBoxFor(m => m.formato, new { type = "file" })
</td>
</tr>
<tr>
<td>Gender: </td>
<td>
@Html.DropDownListFor(m => m.wiw, new List
<SelectListItem>
{ new SelectListItem{Text="India", Value="India"}, new SelectListItem{Text="China", Value="China"}, new SelectListItem{Text="Australia", Value="Australia"}, new SelectListItem{Text="France", Value="France"}, new SelectListItem{Text="Unites States", Value="Unites
States"}, new SelectListItem{Text="Russia", Value="Russia"}, new SelectListItem{Text="Canada", Value="Canada"}}, "Please select")
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
}
Muchas gracias a todos por tu apoyo y su tiempo :)
Este es mi ajax que cambié y con el que trato de ejecutar el mismo método de C#
< 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>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>
test.Files
, el error que te muestra el IDE es porque estás intentando hacer uso de un objeto nulo ;)