I've got a CSHTML form in Visual Studio 2017 looking to save attachments in a folder called "YourRightsFiles" in the main project folder and store details in a database for a blog/message board for the website the form is related to. Based off of the code shown under the section "Uploading Image" in the following Microsoft tutorial - https://learn.microsoft.com/en-us/aspnet/web-pages/overview/ui-layouts-and-themes/9-working-with-images - I wrote the following code for the form:
<div id="creds" style="margin-top:50px;float:right;visibility:hidden;height:0">
<form method="post" enctype="multipart/form-data">
<span style="font-weight:bold">First Name: </span><br> <input type="text" name="first name" required><br><br>
<span style="font-weight:bold">Last Name: </span><br> <input type="text" name="last name" required><br><br>
<span style="font-weight:bold">Job Title: </span><br> <input type="text" name="job title" required><br><br>
<span style="font-weight:bold">Subject: </span><br> <input type="text" name="subject" cols="30" required><br><br>
<div id="attachments">
<span style="font-weight:bold">Attachments (images and videos only): </span><br> <input type="file" accept="video/*, image/*" name="attachment0" cols="30"><br><br>
<input onclick="return add_attachment()" readonly value="Add Another Attachment" style="width:auto;background-color:lightgrey;border-color:black;border-width:1px;margin-bottom:30px;padding:3px;border-radius:6px;"><br><br>
<script>
function add_attachment() {
var count = 0;
var input_tags = document.getElementsByTagName("input");
for (var i = 0; i < input_tags.length; i++) {
if (input_tags[i].name.search("attachment") != null) {
count = count + 1;
}
}
var attach_html = "<input type=\"file\" accept=\"video/*, image/*\" name=\"attachment" + count.toString() + "\" cols=\"30\"><br><br>";
document.getElementById("attachments").innerHTML = document.getElementById("attachments").innerHTML + attach_html;
}
</script>
</div>
<span style="font-weight:bold">Message: </span><br> <textarea type="text" name="message" rows="4" cols="30" required></textarea><br><br>
<input type="submit" name="Submit" readonly value="Submit Message" style="width:auto;background-color:lightgrey;border-color:black;border-width:1px;margin-bottom:30px;padding:3px;border-radius:6px;">
</form>
</div>
@if (IsPost)
{
var connectionString = "MY CONNECTION STRING";
var providerName = "System.Data.SqlClient";
var db = Database.OpenConnectionString(connectionString, providerName);
if (Request.Form["Submit"] == "Submit Message")
{
var firstName = Request.Form["first name"];
var lastName = Request.Form["last name"];
var jobTitle = Request.Form["job title"];
var subject = Request.Form["subject"];
var message = Request.Form["message"];
var all_fields = Request.Form.AllKeys;
System.Diagnostics.Debug.Write(all_fields);
var insertQuery = "insert into YourRights values(@0,@1,@2,@3,@4)";
foreach(var field in all_fields) {
if (field.ToString().Contains("attachment")) {
WebImage photo = WebImage.GetImageFromRequest(field.ToString());
var newFileName = "";
var imagePath = "";
if (photo != null)
{
newFileName = Guid.NewGuid().ToString() + "_" +
Path.GetFileName(photo.FileName);
imagePath = @"YourRightsFiles\" + newFileName;
photo.Save(@"~\" + imagePath);
}
}
}
db.Execute(insertQuery, firstName, lastName, subject, message, jobTitle);
}
db.Close();
}
The code does not seem to save the file within "YourRightsFiles". I also tried just saving inside the main project folder with:
imagePath = @"\" + newFileName;
But that didn't work either. All the database insert related code is working, so there's no issue with form submission, and I am not getting any errors at all during demoing, not even in the console. I'm not sure what I'm doing wrong here