Re: ASP.NET with SQL image type for up/down...
From: Dan Guzman (guzmanda_at_nospam-online.sbcglobal.net)
Date: 10/06/04
- Next message: David Gugick: "Re: Design alternatives for row possibly larger than 8k"
- Previous message: Sridhar K: "Re: Transaction replication"
- In reply to: TJ: "ASP.NET with SQL image type for up/down..."
- Messages sorted by: [ date ] [ thread ]
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.
>
>
>
- Next message: David Gugick: "Re: Design alternatives for row possibly larger than 8k"
- Previous message: Sridhar K: "Re: Transaction replication"
- In reply to: TJ: "ASP.NET with SQL image type for up/down..."
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|