如果文件字段为空,则需要传递默认图像. [英] If file field is empty, I need to pass the default Image.
本文介绍了如果文件字段为空,则需要传递默认图像.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的应用程序中有一个图像字段,如果用户传递了图像,则将其存储到数据库中,并且可以正常工作.现在我的问题是,如果用户不传递图像,那么我需要传递存在于我的图像文件夹中的默认图像.我已经写了如下代码....
Hi,
I am having a image field in my app, if the user pass the image, i am storing it to the database and its working fine. Now my problem is, if the user not pass the image, then i need to pass the default image that is present in my images folder. I have written the code like....
if (fuDoctorImage.HasFile)
{
byte[] img = new byte[fuDoctorImage.PostedFile.ContentLength];
HttpPostedFile DocImg = fuDoctorImage.PostedFile;
DocImg.InputStream.Read(img, 0, fuDoctorImage.PostedFile.ContentLength);
SqlParameter Uploading = new SqlParameter("@Image", SqlDbType.Image);
Uploading.Value = img;
Dcmd.Parameters.Add(Uploading);
Dcmd.Parameters.AddWithValue("@ImageName", fuDoctorImage.FileName);
}
else
{
//I need the code for this section that pass the default image.
}
请帮帮我..我的数据库中有两个字段
ImageType nvarchar(20)null;
图片图片null;
Please help me out.. I am having two fields in my database
ImageType nvarchar(20) null;
Image image null;
推荐答案
hi,
在您的else
部分中,将默认图像转换为字节,然后像这样传递到数据库:
in yourelse
section convert the default image into byte and pass to database like this:
FileStream fs = new FileStream(fuDoctorImage.FileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] image = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
并像
一样传递
and pass it like
Uploading.Value = image;
希望对您有帮助.
hope it will help you.
这篇关于如果文件字段为空,则需要传递默认图像.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文