Re: ASP.NET with SQL image type for up/down...

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Dan Guzman (guzmanda_at_nospam-online.sbcglobal.net)
Date: 10/06/04


Date: Tue, 5 Oct 2004 23:34:08 -0500

You might compare the original file size with the data length stored in the
database:

SELECT filelength, DATALENGTH(filecontent)
FROM MyTable

If there is a mismatch, then the issue is must be related to HttpPostedFile.
I don't know much about ASP.NET so I can't say whether or not this is the
culprit by looking at your code. In case it helps, the snippet below copies
the contents of a binary in/out of a database table. The file sizes were
identical in my test.

 try
 {
    SqlConnection connection =
        new SqlConnection(connectionString);
    connection.Open();
    SqlCommand command =
        new SqlCommand("CREATE TABLE #temp(filecontent image)", connection);
    command.ExecuteNonQuery();

    //copy image from file to database
    byte[] fileContentBuffer = new byte[10000000]; // 10 MB max size
    byte[] fileContent;
    command.CommandText =
        "INSERT INTO #temp VALUES(@filecontent)";
    SqlParameter sqlParameter =
        new SqlParameter("@filecontent", SqlDbType.Binary);
    command.Parameters.Add(sqlParameter);
    FileStream inFile =
        new FileStream(@"C:\TestFileIn.zip", FileMode.Open);
    int fileContentLength =
        inFile.Read(fileContentBuffer, 0, fileContentBuffer.Length);
    fileContent = new byte[fileContentLength];
    Array.Copy(fileContentBuffer, fileContent, fileContentLength);
    sqlParameter.Value = fileContent;
    command.ExecuteNonQuery();
    inFile.Close();

    //copy image from database to file
    FileStream outFile =
        new FileStream(@"C:\TestFileOut.zip", FileMode.OpenOrCreate);
    command.CommandText = "SELECT filecontent FROM #temp";
    SqlDataReader sqlDataReader = command.ExecuteReader();
    if(sqlDataReader.Read())
    {
        fileContent = sqlDataReader["filecontent"] as byte[];
        outFile.Write(fileContent, 0, fileContent.Length);
    }
    outFile.Close();
    connection.Close();
 }
 catch (SqlException ex)
 {
  Console.WriteLine(ex.ToString());
 }
 catch (Exception ex)
 {
  Console.WriteLine(ex.ToString());
 }
 Console.WriteLine("Done");
 Console.ReadLine();
}

-- 
Hope this helps.
Dan Guzman
SQL Server MVP
"TJ" <TJ@discussions.microsoft.com> wrote in message 
news:5E14EEDE-B816-48AC-9A80-85C82A04937F@microsoft.com...
> Hi,
>
> I've written code web-based uploading and downloading.
> Here is some code for it.
>
> For saving file into MS-SQL database,
> SaveFileIntoDB(HttpPostedFile file) {
>            int fileLength = file.ContentLength;
>            byte[] fileContent = new byte[fileLength];
>            int lastPos = file.FileName.LastIndexOf('\\');
>            fileName = file.FileName.Substring(++lastPos);
>            file.InputStream.Read(fileContent,0,fileLength);
>            String fileContentType = file.ContentType;
>
>            // Save fileLength,fileName and fileContent into MS-SQL 
> database
>            // data type of fileLength(column name : filelength) is int
>            // data type of fileName(column name : filename) is 
> varchar(100)
>            // data type of fileContent(column name : filecontent) is 
> image.
>            // data type of fileContent type(column name : contenttype) is
> varchar(100)
>
>            DB.Save(......);
>
> }
>
> For downloading the file from the DB.
>
> Download() {
>
>                // Fetch the file info into db variable from the DB.
>
> int filesize = Convert.ToInt32(db["filesize"].ToString());
>                byte[] fcontent = db["filecontent"] as byte[];
> ms = new MemoryStream();
> ms.Position = 0;
> ms.Write(fcontent,0,fcontent.Length);
> Response.AppendHeader("Content-Disposition",
>     "filename=" + db["filename"].ToString());
> Response.ContentType = db["contenttype"].ToString();
> Response.OutputStream.Write(ms.GetBuffer(),0,Convert.ToInt32(ms.Length));
> }
>
> For saving the file info to the sql db, it worked as I expected. Also, 
> when
> I tried to download the file that I saved into the db, it worked, HOWEVER,
> when I checked the filesize, it was DIFFERENT. Do you have any idea why?
>
> For example,
>
> Suppose that I uploaded "A.exe" file into the sql db.
> The file size of A. exe was 5113 Bytes.
> If I downloaded the A.exe, and then when I checked the file size,
> it was 5,896 Bytes.
> I think that it should be same size since it was same file.
> Always whenever I download the file that I've uploaded, the file size is
> slighlty bigger than uploaded file. I really couldn't find any reason....
> After downloading, the file seems like working well, but I guess it must 
> be
> something wrong since it was different file size.
>
> If you could give any ideas about this issue, I really appreciate it.
>
> Thanks.
>
>
> 


Relevant Pages

  • Re: File upload/download from database. Download appends aspx page to end of file
    ... | I am trying to upload a file and save it in a Sql Server 2000 database. ... when I download the file from SQL ... What follows is my code for uploading and downloading a ... | out attachmentFileName, out attachmentMimeType)) ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: How to create a file folder using ASP.NET 2.0 ?
    ... uploading and downloading. ... A user can create a folder and sub folders for himself on a web page, ... database), or stored directly in a database, as Bjorn has told you. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: File upload
    ... FIles are being stored as blobs in the sharepoint content database, to be precise in the COntent column of the Docs table. ... or save a file copy in SQL Server after uploading it. ... How does SPS ...
    (microsoft.public.sharepoint.portalserver)
  • ASP.NET with SQL image type for up/down...
    ... I've written code web-based uploading and downloading. ... For saving file into MS-SQL database, ... For saving the file info to the sql db, ...
    (microsoft.public.sqlserver.programming)
  • weird....different size between uploading and downloading???
    ... I've written code web-based uploading and downloading. ... For saving file into MS-SQL database, ... For saving the file info to the sql db, ...
    (microsoft.public.dotnet.framework.aspnet)