任何一个指南都可以作为将图像存储到数据库的最佳方式吗?我正在使用Sql server 2008.目前我在数据库中存储文件路径,但是当我尝试获取它时,它不会被提取。
以下是保存图片的代码:
string Extention = System.IO.Path.GetExtension(fuBanner.FileName);
if (Extention == ".jpg" || Extention == ".jpeg" || Extention == ".bmp" || Extention == ".PNG")
{
result.Banner_SignUp_Page = System.IO.Path.GetExtension(fuBanner.FileName);
db.SubmitChanges();
lblMsg.Visible = true;
Response.Redirect("ListOfEvent.aspx");
//lblMsg.Text = "Records updated successfully.";
}
获取图片的代码:
var result1 = from a in db.EMR_INVITATIONs
join b in db.EMR_EVENTs on a.EventID equals b.EventID
where b.EventID == (int)Session["eventid"]
select new
{
Banner = b.Banner_SignUp_Page,
Title = b.Title
};
答案 0 :(得分:4)
在你的情况下:
以下是您想要实现的目标的代码:
protected void btnUpload_Click(object sender, EventArgs e)
{
if (fuFileUploader.HasFile && fuFileUploader.PostedFile.ContentLength > 0)
{
string path_file_name = fuFileUploader.PostedFile.FileName;
string ext = Path.GetExtension(path_file_name).Replace(".", "");
string file_name = Path.GetFileNameWithoutExtension(path_file_name);
string file_title = txtFileTitle.Text.Trim();
HelperDataClassesDataContext db = new HelperDataClassesDataContext();
try
{
byte[] file_byte = fuFileUploader.FileBytes;
Linq.Binary file_binary = new Linq.Binary(file_byte);
ControlDocument cd = new ControlDocument
{
guid = Guid.NewGuid(),
file_ext = ext,
file_nm = file_name.Trim(),
file_title=file_title,
file_bin = file_binary,
is_active = true,
upload_page_type = rblLocation.SelectedValue,
upload_dt = DateTime.Now,
upload_by = UtilUniverse.Common.CurrentUserLogin.Trim()
};
db.ControlDocuments.InsertOnSubmit(cd);
}
finally
{
db.SubmitChanges();
}
}
}
答案 1 :(得分:4)
在将图像保存到磁盘之前调整大小的代码。它只保存数据库中的路径而不是数据库中的图像。如果我们在磁盘上的数据库和文件中保存路径,那么每当我们必须显示该图像时,我们可以节省CPU成本(试一试)。您可以使用它来保存项目的任何图像,只需提供上传器控件和图像路径即可。
public static bool SaveImage(HttpPostedFile imageUpload, string imagePath, out string error)
{
error = "";
string extension = Path.GetExtension(imageUpload.FileName);
if (extension.ToLower() == ".jpg" || extension.ToLower() == ".jpeg" || extension.ToLower() == ".png")
{
try
{
System.Drawing.Bitmap objImage = new System.Drawing.Bitmap(imageUpload.InputStream);
System.Drawing.Bitmap outImage = new Bitmap(170, 200);
System.Drawing.Graphics outGraphics = Graphics.FromImage(outImage);
SolidBrush sb = new SolidBrush(System.Drawing.Color.White);
outGraphics.FillRectangle(sb, 0, 0, 170, 200);
outGraphics.DrawImage(objImage, 0, 0, 170, 200);
sb.Dispose();
outGraphics.Dispose();
objImage.Dispose();
outImage.Save(System.Web.HttpContext.Current.Server.MapPath(imagePath));
return true;
}
catch (Exception e)
{
error = "Failed to save Image. Error: " + e.Message;
}
}
else
{
error = "Invalid image foramt.";
}
return false;
}
然后您可以通过获取图像路径来获取它
即
image.url = //图像列值
答案 2 :(得分:1)
InstituteRegistration insti = new InstituteRegistration();
if (FileUploadInstituteImage.PostedFile.ContentLength != 0)
{
string extension = System.IO.Path.GetExtension(FileUploadInstituteImage.FileName);
if (extention.ToLower() == ".jpg" || extention.ToLower() == ".jpeg" || extention.ToLower() == ".gif" || extention.ToLower() == ".png" || extention.ToLower() == ".bmp" || extention.ToLower() == ".tif" || extention.ToLower() == ".tiff")
{
Stream fsInstitute = FileUploadInstituteImage.PostedFile.InputStream;
BinaryReader brInsti = new BinaryReader(fsInstitute);
Byte[] bytesInstitute = brInsti.ReadBytes((Int32)fsInstitute.Length);
insti.InstituteImage = bytesInstitute;
}
else
{
lblMsg.Text = "Invalid File.";
return;
}
}
db.InstituteRegistrations.InsertOnSubmit(insti);
db.SubmitChanges();