Hello, I'm doing a module in ASP.NET with Razor and C # where I upload a file of any type to a database , along with other data. I need to get the properties of the file on the server side ("with C # "), in order to be able to append these new data in my query to have them in the table.
This is the error that appears to me when I try to get the data.
This is my client and server side code.
[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>
}
Thank you very much everyone for your support and your time :)
This is my ajax that I changed and with which I try to execute the same method of 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>