2

How to save image path or name to database and copy the image in a folder. I have searched many pages on the internet but no luck. Please can anyone show my how to create the controller?

My View

<form action="Create" method="post">
    <table>
        <tr>
            <td>
                <input type="date" name="InquirDate" id="InquirDate" placeholder="Date" />
            </td>
            <td>
                <input type="text" id="signby" name="signby" placeholder="signby" />
            </td>
            <td>
                <input type="text" id="description" name="description" placeholder="Description" />
            </td>
            <td>
                <input type="text" id="parentInqNo" name="parentInqNo" placeholder="Parent Inquiry number">
            </td>
            <td>
                <select id="status" name="status">
                    <option>Draft</option>
                    <option>Published</option>
                </select>
            </td>
            <td>
                <input type="text" id="number" name="number" placeholder="number" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="subject" id="subject" placeholder="Subject" />
            </td>
            <td>
                <input type="text" id="empid" name="empid" placeholder="empid" />
            </td>
            <td>
                <input type="file" name="Attachment" id="Attachment" />
            </td>
            <td>
                <input type="text" name="InqiurURL" id="InqiurURL" placeholder="InqiurURL" />
            </td>
            <td>
                <input type="text" name="EnterDate" id="EnterDate" placeholder="EnterDate" />
            </td>
        </tr>
    </table>
    <input type="submit" value="Save">
</form>

1 Answer 1

1

Try following this code in your controller

public ActionResult Save(FormCollection fc, HttpPostedFileBase Attachment)//Instead of Attachment better to use "file"   
    {  
            if (Attachment!= null)
            {
              Database1Entities db = new Database1Entities();
              string ImageName = System.IO.Path.GetFileName(Attachment.FileName);
              string physicalPath =Server.MapPath("~/images/"+ ImageName);

              // save image in folder
              Attachment.SaveAs(physicalPath);

              //save new record in database
               tblA newRecord = new tblA();
               newRecord.empid= fc.empid;
               newRecord.subject= fc.subject;
               //....Add all field which need to save in db
               newRecord.Attachment= ImageName;
               db.tblA.Add(newRecord);
               db.SaveChanges();

            }
            return View();  
      }  
Sign up to request clarification or add additional context in comments.

1 Comment

thanks with little modification it worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.