Upload image to database

Last post 12-17-2007, 5:00 PM by sparkymark75. 8 replies.
Sort Posts: Previous Next
  •  12-12-2007, 4:57 PM 35789

    Upload image to database

    I've downloaded the latest sample for uploading images to a SQL database. I've included the SqlFileStorage.cs file in my APP_Code directory (ASP.NET 2.0 web app), updated the connection string and created a downloadImage.aspx file with the code to download the image from the database.
     
    I can browse the images (there are none in the database) but when I try and upload an image, I get the following error straight after picking an image to upload.
     
    Failed to map the path '/image.png'
     
    My code in the page that uses CE is;
     

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not Page.IsPostBack Then

    newsItemBodyContent.Setting("CuteEditorFileStorageType") = GetType(SqlImageStorage).AssemblyQualifiedName

    newsItemBodyContent.Setting("DownFile") = ResolveUrl("downloadImage.aspx")

    newsItemBodyContent.SetSecurityGalleryPath("/")

    End If

    End Sub

  •  12-15-2007, 5:58 PM 35823 in reply to 35789

    Re: Upload image to database

    The developer who is responsible for this part will check this issue for you soon.


    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

  •  12-16-2007, 2:53 PM 35833 in reply to 35789

    Re: Upload image to database

  •  12-17-2007, 6:00 AM 35841 in reply to 35833

    Re: Upload image to database

    Thanks for the reply Adam.
     
    I'm no longer having the problem above, however, when trying to upload an image, I receive an IE dialog box stating; "Unable to upload file." If I run SQLProfiler whilst doing this, it appears that it's at least attempting to write the image to the database, the following is shown in SQLProfiler..
     
    exec sp_executesql N'insert into fsitems (path,isdir,parentpath,createdt,filename,filesize,filedata) values (@cdq_0,0,@cdq_1,CONVERT(DATETIME,@cdq_2),@cdq_3,@cdq_4,@cdq_5)',N'@cdq_0 nvarchar(19),@cdq_1 nvarchar(1),@cdq_2 nvarchar(50),@cdq_3 nvarchar(18),@cdq_4 int,@cdq_5 varbinary(5329)',@cdq_0=N'/lightning_logo.png',@cdq_1=N'/',@cdq_2=N'2007-12-17 11:54:29.671',@cdq_3=N'lightning_logo.png',@cdq_4=5329,@cdq_5=0x89504E470D0A1A0A0000000D49484452000000...
     
    But no rows are in the fsitems table.

    Is this likely to be some sort of permissions issue?
  •  12-17-2007, 6:08 AM 35842 in reply to 35841

    Re: Upload image to database

    If I take the query shown in SQLProfiler and run it in QueryAnalyzer, I receive the following error;

    Msg 8115, Level 16, State 2, Line 1

    Arithmetic overflow error converting expression to data type datetime.

  •  12-17-2007, 9:15 AM 35844 in reply to 35842

    Re: Upload image to database

    OK, I've got the uploading working by changing the date format in the SQLFileStorage.cs to UK format dates and the image is being saved to the database and displayed fine in the image selector. Trouble now is my downloadimage page isn't returning the image!!
     

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim dt As DateTime

    Dim filename As String

    Dim filesize As Integer

    Dim filedata As Byte()

    Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("northernLeague").ConnectionString)

    conn.Open()

    Dim fileid As Integer = Integer.Parse(Request.QueryString("fileid"))

    Using comm As New SqlCommand("aspnet_League_GetMediaItem", conn)

    comm.CommandType = CommandType.StoredProcedure

    comm.Parameters.Add("MediaID", SqlDbType.Int).Value = Convert.ToInt32(fileid)

    Using reader As SqlDataReader = comm.ExecuteReader()

    If Not reader.Read() Then

    Throw (New HttpException(404, " File Not Found "))

    End If

    dt = reader.GetDateTime(reader.GetOrdinal("Createdt"))

    filename = reader.GetString(reader.GetOrdinal("Filename"))

    filesize = reader.GetInt32(reader.GetOrdinal("Filesize"))

    filedata = CType(reader("Filedata"), Byte())

    End Using

    End Using

    End Using

    Response.ClearHeaders()

    'Response.ContentType = "image/gif"

    Response.AddHeader("Content-Length", filesize.ToString())

    Response.BinaryWrite(filedata)

    Response.End()

  •  12-17-2007, 9:37 AM 35849 in reply to 35844

    Re: Upload image to database

    Hi,
     
    I think your code is correct.
     
    What error if you navigate to that downloadimage.aspx?fileid=xxx ?
     
     
    By the way , are you using ASP.NET2.0 ?
     
    If you use theme , make sure you change the <head> to <head runat="server"> in the downloadimage.aspx
     
     
    Regards , Terry .
  •  12-17-2007, 4:58 PM 35860 in reply to 35849

    Re: Upload image to database

    Hi Terry,
     
    If I navigate to the page with the querystring, I just get a blank page, no error, no nothing!
     
    I am using ASP.NET 2.0 but am not using a theme. I'm using MasterPages but the head tag in the MasterPage has it's head tag set as a server control (<head runat=server>)
     
    The ASP.NET markup for my downloadImage.aspx page is;
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="downloadImage.aspx.vb" Inherits="downloadImage" %>
     
    The stored procedure works fine if you run it directly so there's something not working from the ASP.NET perspective.
  •  12-17-2007, 5:00 PM 35861 in reply to 35860

    Re: Upload image to database

    Strange, just tried it on another machine (my laptop) and it works fine!!!
View as RSS news feed in XML