0

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

Luv Tomar
  • 127
  • 2
  • 13
  • 1
    You should be writing files to the App_Data folder. The default app pool identity does not have rights to write to the webroot folder as it shouldn't for security reasons – hawkstrider Sep 23 '19 at 16:18
  • I actually don't have an App_Data folder that was automatically created when I first started the project. Is there anywhere else I should be saving it? – Luv Tomar Sep 23 '19 at 17:10
  • Just add the App_Data folder. Right click on project -> Add -> Add ASP.Net Folder -> App_Data – hawkstrider Sep 23 '19 at 17:42
  • I did that, and added a "YourRightsFiles" folder to it. It's still not saving the uploaded image. – Luv Tomar Sep 23 '19 at 21:06
  • I also tried just directly saving it in the App_Data folder. Didn't work either. – Luv Tomar Sep 23 '19 at 21:09
  • Have you checked the folder permissions and added any error handling? – hawkstrider Sep 24 '19 at 11:39
  • I've updated App_Data's and any folders' (inside of App_Data) permissions to include read and write for IIS use. Still not working. And I'm not sure what you mean by error handling when there's no error being produced anywhere (including the console). – Luv Tomar Sep 25 '19 at 13:24

0 Answers0