View Attachments uploaded from Forms
- Last UpdatedNov 24, 2023
- 1 minute read
You can view attachments uploaded from forms in ASPX pages using scripts as follows.
Here we will view a photograph uploaded from a form in an ASPX page.
-
Create a list and the dynamic list form.
-
Populate the list using the dynamic list form, and also upload the photograph using the attachment control on the form.
-
Create an ASPX page with a button control.
-
Code the on click event for the button as follows:
protected void Button_Click(object sender, EventArgs e)
{
securedValue.HeaderPersisted = true;
this.LoadFileContent(true, "Emp001");
}
private void LoadFileContent(bool isHeaderPersisted, string title)
{
var dbConnection = new SqlConnection();
try
{
int startIndex = 0;
if (isHeaderPersisted)
{
AttachmentHeader header = new AttachmentHeader();
// Repository Name
header.ApplicationName = "Forms Repository";
// Attachment File Name
header.FileName = securedValue.FileName;
header.HeaderVersion = 1;
byte[] info = header.BuildHeader();
startIndex = info.Length;
}
// Connection string
dbConnection.ConnectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
// SQL command for querying the database table
var command = new SqlCommand("select Id, Photograph from Employee where Title=@title", dbConnection);
// Parameter for the title column
command.Parameters.AddWithValue("@title", title);
// Open the database connection
dbConnection.Open();
// Execute reader
var reader = command.ExecuteReader();
// Read the data from reader and write the same into the Context.Response
if (reader.HasRows)
{
while (reader.Read())
{
Byte[] bytes = (Byte[])reader.GetValue(1);
var ms = new MemoryStream(bytes);
Byte[] thumbnailByteArray = new Byte[ms.Length];
ms.Position = startIndex;
ms.Read(thumbnailByteArray, 0, Convert.ToInt32(ms.Length));
// File type
Response.ContentType = "application/jpg";
Response.BinaryWrite(thumbnailByteArray);
Response.Flush();
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
// Close the database connection
dbConnection.Close();
}