0
Answer

Displaying an image from a directory

Luis

Luis

16y
2.1k
1

I neeed to display an image that is in a directory folder. That image has a name with .jpg the name starts with numbers that are the id from the table(123456.jpg). I got this err:err:'string' does not contain a definition for 'ImageUrl' is at the bottom of the code. I am converting an old ASP code to c#.

This is the actual code in ASP:

<%
fShot = False
Set fs = Server.CreateObject("Scripting.FileSystemObject")
If fs.FileExists("c:\inetpub\froot\" & sN & ".jpg") = True Then
fs.CopyFile "c:\inetpub\ftproot\" & sN & ".jpg", "d:\AB\Web\images\" & sN & ".jpg"
If Err.Number <> 0 Then
response.write "<!--Error: " & Err.Description & "-->"
Err.Clear
Else
fShot = True
End If
End If
Set fs = Nothing
If fShot = True Then
%>
-----------------------------------------------------------------

This is my code in c#:

    public void getPH()
    {
        string imgPH;
        string myConn = "Server=myserver;DataBase=mydb;uid=,myud;pwd=mypass;";
        string strQuery = "myStoredProcedure";
        string pth;

        SqlConnection cn = new SqlConnection(myConn);

        SqlCommand myCommand = new SqlCommand(strQuery, cn);
        myCommand.CommandType = CommandType.StoredProcedure;
        myCommand.Parameters.AddWithValue("@tableID", cn);
        cn.Open();
       
        byte[] imageData = (byte[])myCommand.ExecuteScalar(); string path;
       
        MemoryStream memStream = new MemoryStream(imageData);
 
 //Folder to look for image
        path = @"c:\inetpub\root\";

 //Folder to write image if exists
        pth = @"d:\AB\Web\images\";
        pth = pth + ".jpg";

        DirectoryInfo dir = new DirectoryInfo(path);

        try
        {
            FileInfo[] fi = dir.GetFiles();
            if (fi.Length != 0)
            {
                if (File.Exists(path))
                {
                    File.Copy(path, pth);
                }
            }
        }
        catch (Exception ex)
        {

            this.lblPHerr.Text = ex.Message;
        }

        if (pth != null)
        {
            string strFileName = pth.ToString();

            if (File.Exists(strFileName))
            {
                FileStream fileStream1 = new FileStream(strFileName, FileMode.Open);
                byte[] byteFile = new byte[fileStream1.Length];
                string[] fileEntries = Directory.GetFiles(path);
                foreach (string path2 in fileEntries)

                fileStream1.Read(byteFile, 0, byteFile.Length);
                fileStream1.Close();

                imgPH = @"images/.jpg";

                Response.ContentType = imgPH;
                Response.OutputStream.Write(byteFile, 0, byteFile.Length);
                Response.End();
            }
            {
                //err:'string' does not contain a definition for 'ImageUrl'
                imgPH.ImageUrl.ToString(strFileName, pth);

            }
        }
    }