AsyncFileupload导致将空图像插入数据库? [英] AsyncFileupload causes empty Image insert into database?

查看:85
本文介绍了AsyncFileupload导致将空图像插入数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,专家,我最近尝试通过asp.net中的 AsyncFileupload 控件插入图像.事情是一切正常,但数据库中的所有图像(数据类型图像)都存储为0x000000000000000 ...这意味着它们都是空字节,对吗?那里没有可读数据?
我为asp.net的文件上传控制编写了相同的代码,并且效果很好,图像字节显然为0xfe543werrrr ..... smth ...我真的不知道问题出在哪里....这是我的代码

Hello experts, i recently tried to insert images via the AsyncFileupload control in asp.net. The Thing is everything works fine but all the Images(datatype Image) in the database are stored as 0x000000000000000...which means they are all null bytes right?? that there is no readable data there right??
I wrote the same code for Fileupload control of asp.net and it works well and the image bytes are clearly 0xfe543werrrr.....smth...I don''t really know what the problem is....here are my codes

protected void btnSubmit_Click(object sender, EventArgs e)
    {

        byte[] bytImageBytes = null;
        System.IO.Stream myStream;
        Int32 nFileLength;
        if (AsyncFileUpload1.PostedFile != null && AsyncFileUpload1.PostedFile.FileName != null)
        {
            nFileLength = AsyncFileUpload1.PostedFile.ContentLength;
            bytImageBytes = new byte[nFileLength];

            myStream = AsyncFileUpload1.PostedFile.InputStream;
            myStream.Read(bytImageBytes, 0, nFileLength);
            



            if (dbOps.InsertBodInformation(txtFullName.Text, txtDesig.Text, txtShortDesc.Text,
                txtLongDesc.Text, bytImageBytes))
            {
                Response.Write(@"<script language='javascript'>
                           alert('Insertion Successful!')</script>");
            }
            else
            {
                Response.Write(@"<script language='javascript'>
                           alert('Error Writing to the database!')</script>");
            }

        }
}



InsertBodInformation函数返回布尔类型...并返回true.
请帮帮我.
在此先感谢
Mlimbu



The InsertBodInformation function returns bool type...and returns true.
Please help me out.
Thanks in Advance
Mlimbu

推荐答案

这可能对您有用.

与其尝试一次读取整个文件,不如分块读取.
This may work for you.

Instead of trying to read the whole file all at once, read it in chunks.
myStream = AsyncFileUpload1.PostedFile.InputStream;
int bufferSize = 1024;
long bytesRead = 0;
int curPos = 0;

while (bytesRead < nFileLength)
{
    bytesRead += myStream.Read(bytImageBytes, curPos, bufferSize);
    curPos += bufferSize;
}


大家好,我解决了我自己的问题.我将其发布在此处,以便任何遇到此问题的人都能有所建树.
关于如何从AsyncFileUpload控件读取二进制数据,这里有很好的参考... http://forums.asp .net/t/1520272.aspx/1 [ ^ ].
我需要做的就是为流obj分配Async控件的FileContent ... like
Hello everyone, I solved my own question. I''m posting this here so that anyone facing this issue can get some idea.
There is a good reference here on how to read binary data from the AsyncFileUpload Control...http://forums.asp.net/t/1520272.aspx/1[^].
All i needed to do was to assign the stream obj with the FileContent of the Async control...like
System.IO.Stream myStream;

        myStream = AsyncFileUpload1.FileContent;
        myStream.Position = 0;

        System.IO.BinaryReader br = new System.IO.BinaryReader(myStream);

        byte[] bytImageBytes = br.ReadBytes((int)myStream.Length);


并将流的开始位置设置为0,并使用二进制读取器从字节数组中读取二进制值.
我仍然没有弄清楚为什么我的初始代码无法正常工作?? ....即异步控件的输入流....请分享一下,如果您知道原因. 谢谢,
Mlimbu


and set the starting position of the stream to 0 and use a binary reader to read the binary values int a byte array.
I still haven''t figured out why my initial code did not work??....i.e the inputstream of the async control....Please, share if you know the reason why.
Thanks,
Mlimbu


这篇关于AsyncFileupload导致将空图像插入数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆