I am using tinymce RichTextBox editor to post my articles. The Texts are working fine but when I add an image to describe the article, image icon shows, the image does not show.
This is how the display is. Image icon show instead of image itself.
This is how the image and texts are stored in my database.
I created images
folder in the solution directory called imgarticle, where the image is saved to be displayed on the web page.
How do I solve this please, or is there another way of doing this?
<asp:TextBox ID="txtTitle" runat="server" CssClass="form-control" />
<br>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="BtnUpload_Click" />
<br>
<asp:TextBox ID="txtHTMLContent" runat="server" TextMode="MultiLine"/>
<script src="https://cdn.tiny.cloud/1/4m42h3gnbjg6lt9vj7w56g7pkunm8l48s1p2p79i0gjga5i4/tinymce/6/tinymce.min.js" referrerpolicy="origin"></script>
<script>
tinymce.init({
selector: '#txtHTMLContent',
plugins: 'advlist link image lists'
});
</script>
<br>
<asp:Button ID="SubmitBtn" Text="Post Article" CssClass="btn btn-primary" runat="server" OnClick="SubmitBtn_Click" />
C#
protected void BtnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
string FilePath = "imgarticle/" + FileName;
FileUpload1.SaveAs(Server.MapPath(FilePath));
txtHTMLContent.Text += string.Format("<img src = '{0}' alt = '{1}' />", FilePath, FileName);
}
}
protected void SubmitBtn_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string query = "INSERT INTO MyArticles (Title, Body, DatePosted) VALUES (@Title, @Body, @DatePosted)";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@Title", txtTitle.Text);
cmd.Parameters.AddWithValue("@Body", txtHTMLContent.Text);
cmd.Parameters.AddWithValue("@DatePosted", DateTime.UtcNow);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
txtHTMLContent.Text = "";
}
}
}
catch (SqlException ex)
{
string msg = "Error:";
msg += ex.Message;
throw new Exception(msg);
}
}