在asp.net的文件上传控件中没有选择图像文件时,如何将默认图像上传到数据库 [英] how to upload default image into database when no image file is selected from fileupload control in asp.net

查看:76
本文介绍了在asp.net的文件上传控件中没有选择图像文件时,如何将默认图像上传到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..

当我的网页中没有从文件上传控件上传图像文件时,我想将默认图像以二进制格式存储到数据库表中.我不想在图像" 字段中插入空值我在Google上搜索的图像表,但找不到解决方案.

我尝试过的代码示例是

Hi..

I want to store a default image in binary format,into database table when no image file is uploaded from fileupload control in my web page.i don''t want to insert null values in "image" field of image table I have searched on google but couldn''t find solution.

code sample i have tried is

if(imgfileupload.hasfile)
{
//here i have converted the image file in fileupload control into byte[]
}
else
{
//here i want code for converting default image into byte[]
i.e if(imgfileupload.hasfile=="false")......
}


请我要源代码.请我要源代码.我真的需要它......

在此先感谢您


please I want source code.please i want source code.i really need it......

Thanks in advance

推荐答案

在上载时,请检查FileUpload控件的.HasFile属性(如果选择了文件,则为true),如下所示:
At the time of uploading you check the .HasFile property of FileUpload control (it will be true if file is selected), as following:
if (FileUpload1.HasFile)
            {
                // Upload Browsed File
            }
            else
            {
                // Use Default File
            }



如果发现有用,请接受答案:)



Accept the answer if found useful :)


如果您将图像作为位图,则可以使用此

If you have the image as bitmap, you can use this

private byte[] GetByteArray(Bitmap bitmapImage)
{
    var byteArray = (byte[])TypeDescriptor.GetConverter(bitmapImage).ConvertTo(bitmapImage, typeof(byte[]));
    bitmapImage.Dispose();
    return byteArray;
}




实际上,对于每种情况,您不必将相同的默认映像重复放入数据库中,因此,我认为您应该只存储一次,并针对没有默认映像的每种情况检索它.关于在所关注的列中存储空图像,我认为更好的做法是有一个包含两列的单独表,您将实体主键用作外键,第二列将包含图像,这样,任何带有该表中没有记录没有自己的图像,因此我们获得了仅存储在一个位置的标准默认图像.




And in fact, for each situation you don''t have to be putting the same default image repeatedly in the database, so, I think you should just store it once and retrieve it for every case where there is no default image. With regards to storing null image in the respected column, I believe a better practice is to have a separate table with two columns where you put the entity primary key as foreign key and the second column will contain the image, this way, any item with no record in this table has no image of its own, sow we get the standard default image that is store in just one location.


这篇关于在asp.net的文件上传控件中没有选择图像文件时,如何将默认图像上传到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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