在ASP.net中保存/取出图片入/从SQL数据库(可用于上传图片)

SSM框架---上传多张图片到项目文件夹内,并将图片的路径存到数据库 数据库:所存的图片路径可以为空,不确定要上传几张图片,图片id自增 spring配置文件里配置文件上传解析器  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">                 <property... 阅读详情
一、把图片存入数据库中
 
用到以下几个方面的知识:
1. 使用流对象
2. 查找准备上传的图片的大小和类型
3.怎么使用InputStream方法

插入图片的必要条件
1.#Form 标记的 enctype 属性应该设置成 enctype="multipart/form-data"
2.# 需要一个<input type=file>表单来使用户选择他们要上传的文件,同时我们需要导入 System.IO名称空间来处理流对象

对SqlServer做以下的准备
1.# 需要至少含有一个图片类型的字段的表
2.# 如果我们还有另外一个变字符类型的字段来存储图片类型,那样会更好一些。

窗体控件
1.插入图片用到的是System.Web.UI.HtmlControls.HtmlInputFile控件,我们在webform中放入这个控件,取名为“imgInput”
2.同时再放入一个确认上传按钮“Button1”

程序代码
AddImg,用于返回要上传的图片内容

 1Private Function AddImg(ByVal InputImg As System.Web.UI.HtmlControls.HtmlInputFile, ByVal ImgType As String, ByVal MaxSize As Int64) As Byte()
 2'传入一个htmlinputfile控件,一个上传图片格式和一个上传图片最大值,返回图片的内容,既要写入数据库中的内容,你也可以同时写入图片类型
 3        Dim intImageSize As Int64
 4        Dim strImageType As String
 5        Dim ImageStream As Stream
 6        ' Gets the Image Type
 7   strImageType=InputImg.PostedFile.ContentType
 8        If strImageType <> ImgType Then
 9            Response.Write("<script>alert('图片类型为""')</script>") 'jgp类型为"image/pjpeg"
10            Exit Function
11        End If
12        ' Gets the Size of the Image
13        intImageSize = InputImg.PostedFile.ContentLength
14        If intImageSize > MaxSize Then
15            Response.Write("<script>alert('图片不得大于K')</script>")
16            Exit Function
17        End If
18        ' Reads the Image
19        ImageStream = InputImg.PostedFile.InputStream
20        Dim ImageContent(intImageSize) As Byte
21        Dim intStatus As Integer
22        intStatus = ImageStream.Read(ImageContent, 0, intImageSize)
23        Return ImageContent
24    End Function

示例调用

Dim imageContent() As Byte
       imageContent = AddImg(fileImg, "image/pjpeg", 512000)'上传图片类型为jpg,最大不超过500K

插入数据库

我想这部分就不用写了吧,你可以用任何方式(推荐使用存储过程),将imageContent插入到数据库中类型为image的字段就行了。

二、把图片从数据库中读出

这部分比较简单:

假设img变量是你从数据库中取出的图片内容
那么直接使用
Response.BinaryWrite(img)
就可以将图片输出到页面上了

三:总结

将图片存放在数据库中其实是起到了图片保护的作用,这样就算别人浏览你的机器也看不到你的图片,也可以用来保护重要的图片资料。
 

java:jsp上传图片存数据库和从数据库取出数据显示在jsp 这个是基于ssm知识 一、jsp上传图片存数据库 ①新建个jsp文件,把代码复制粘贴 <%-- Created by IntelliJ IDEA. User: 86153 Date: 2021/3/9 Time: 15:27 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language=" 阅读详情

相关推荐

java实现多文件上传并保存到数据库

好久没有写博客了,今天项目里需要一个功能,即前台实现多图片上传列表,并后台保存到mysql中。前台后台一起弄,用了半天时间终于搞定了。 实现:前台Layui实现;后台servlet+hibernate 不多废话,先上图 前台实现主要是用的layui框架,框架本身提供多文件上传列表,但是没有查看的功能,我自己实现了图片查看的功能,核心代码如下: &lt;div class="lay...

only红塔山 1万+

图片上传到mysql_图片上传到数据库的总结

转眼间在博客园里待了快一年了,我还能记得去年的这个时候老师有个项目,在这个项目里需要把图片保存到数据库里,以前我做的就是把路径保存到数据库,后来为了实现这个功能我可是费了不少劲啊,问东问西的最后还是在优快云网友的帮助下完成了。今天晚上又有优快云网友提问有关图片保存到数据库的问题。下面我来汇总一下如何将图片保存到SqlServer、Oracle、Access数据库中。首先,我们要明白图片是以二进...

weixin_35480435的博客 711

在ASP.NET中把图片保存到SQL SERVER数据库

在ASP.NET中把图片保存到SQL SERVER数据库翻译:吕绍伟介绍在很多情况下,我们需要把图片保存到数据库中。在某些应用程序中,存在一些敏感信息不能被存储到文件系统中,因为存储在文件系统上的任何图片都很容易被用户非法获得。本文将讨论在ASP.NET中怎样把图片保存到SQL SERVER数据库中。在本文中我们将了解到以下几方面的内容:l         上载图片文件的要

comy的专栏 1895

ASP.NET--图片上传保存url到数据库

ASP.NET--图片上传保存url到数据库 开发工具:Visual Studio 2015、SQL Server 2014 Management Studio 关键技术:C# MVC 作者:刘东标 撰写时间:2019年1月18日 实现效果: 、、、、、、、、、、、、、、、、、html代码、、、、、、、、、、、、、 &lt;/table&gt; ...

weixin_44538423的博客 2278

ASP.NET中图片上传功能的简便实现方法

ASP.NET,作为微软.NET框架的一部分,长期以来一直是企业级Web应用开发的首选技术之一。它是基于公共语言运行时(CLR)构建的,这意味着它可以支持多种编程语言,如C#和VB.NET,为开发者提供了极大的灵活性。ASP.NET核心优势在于其能创建动态网页内容,并可以轻松集成至各种Web服务与应用程序中。ASP.NET的设计初衷是为了简化开发过程。它允许开发者使用声明式编程和基于控件的开发模式,这使得开发动态网站和Web应用程序变得更加高效。

weixin_35749796的博客 1080

解决2020R2,2022R1等版本的SCDM无法编辑草图尺寸的问题

解决2020R2,2022R1等版本的SCDM无法编辑草图尺寸的问题

y_u_yu_yu_的博客 1万+

ASP.NET--图片上传通过二进制保存到数据库

ASP.NET–图片上传通过二进制保存到数据库 开发工具:Visual Studio 2015、SQL Server 2014 Management Studio 关键技术:C# MVC 作者:刘东标 撰写时间:2019年1月18日 实现效果: 、、、、、、、、、、、、、html代码、、、、、、 @*选择文件*@ &lt;div cl...

weixin_44538423的博客 1569

Win10 LTSC 2021 CPU占用率飙升?KB5017308补丁修复全攻略

本文针对Windows 10 LTSC 2021系统中因wsappx进程导致的CPU占用率异常飙升问题,提供了详细的解决方案。核心在于安装微软官方发布的KB5017308补丁,该补丁能从根本上修复系统组件缺陷,使CPU占用恢复正常。文章详解了补丁的下载、安装步骤、注意事项及安装后的验证方法,是解决此问题的权威指南。

python9snake的博客 1260

在ASP.net中保存/取出图片入/从SQL数据库

一、把图片存入数据库中 用到以下几个方面的知识:1. 使用流对象2. 查找准备上传的图片的大小和类型3.怎么使用InputStream方法插入图片的必要条件1.#Form 标记的 enctype 属性应该设置成 enctype="multipart/form-data"2.# 需要一个表单来使用户选择他们要上传的文件,同时我们需要导入 System.IO名称空间来处理流对象 对SqlServe

lne818的专栏 1060

从理想光源到部分相干:Abbe与Hopkins模型在计算光刻中的演进与选择

本文深入探讨了计算光刻中Abbe模型与Hopkins模型的演进与选择,分析了理想光源与部分相干光源在光刻仿真中的关键差异。Abbe模型基于理想点光源假设,计算简洁但存在精度局限;Hopkins模型则通过互强度函数和TCC技术,更准确地模拟现实中的部分相干光源,尤其适用于先进节点光刻工艺。文章还提供了模型选择的实用建议,帮助工程师在精度与效率间取得平衡。

weixin_29217235的博客 297

手把手教你使用YOLOv11模型,训练自己数据集,百分百成功(无废话)

YOLOV11百分百成功训练自己的数据集并进行图像、视频流脚本预测

chenhenGzzz的博客 1952

ASP.Net.MVC 图片的保存上传和显示

文献种类:专题技术 开发工具与关键技术:Visual Studio 文献编号:ASP.Net.MVC 图片的保存上传和显示 作者:张家诚 ; 撰写时间 2020 年 8 月 8 日 1:导入用户的头像这个功能不难实现,导入头像其实就是在原有的用户上添加一项新的功能而已需要用到 Ajax 的 post 请求方法和数据库的物理路径的记录,还有并不是所有的用户都可以使用者一项功能那就是超级管理员,超级管理员拥有页面的全部功能的权限一般是给开发人员进行页面调试来使用的是不能出现任何的问题其余的用户就只有一小部分

Cpa678的博客 1992

LaTeX伪代码字体大小调整全攻略:从algorithm到algorithm2e的实战技巧

本文详细介绍了在LaTeX中调整伪代码字体大小的全面技巧,涵盖algorithm、algorithmicx和algorithm2e等主流算法包的使用方法。从基础命令到高级配置,帮助用户解决学术论文和技术文档中的伪代码排版问题,提升算法展示的专业性和可读性。特别针对不同期刊格式要求,提供了实用的字体调整方案。

weixin_29297127的博客 437

ASP.Net数据库如何存取图片

当我们有大量的图片或者图片比较大时,我们常规的做法可能是保存图片路径,但是也不排除需要将图片直接存放到数据库的情况,此时就需要保存图片到数据库了。这篇文章我会向大家介绍: 如何通过FileUpLoad控件将图片保存到数据库 如何通过Button控件从数据库导出图片 具体步骤如下: 保存图片到数据库 第一步:首先在数据库创建一个名为“Images”的表,代码如下: CREATE

缠中说禅的博客 5430

攻克+86手机号Discord验证墙:从“无效”到“成功”的实战指南

本文提供了一份针对+86手机号在Discord注册时频繁遭遇“手机号无效”问题的实战解决指南。文章系统分析了问题根源,并分步详解了从打造纯净网络环境、尝试多种手机号格式(如+86前加0),到更换iOS等客户端设备的有效方法,旨在帮助用户成功完成Discord手机号验证。

mac99的博客 1109

42、使用mmrotate中k3det进行旋转目标检测,并进行mnn部署和ncnn部署

基本思想:仍然是身份证分割,并进行ncnn和mnn的c++的部署开发

sxj731533730 5708

ASP.net之图片上传(保存在SQL数据库)

在网上找不到特定的需求.然后自己试着写了一下.功能算是完成。图片上传数据库,就是把图片文件保存在数据库的一个字段.SQL image格式是个二进制的集合。 先看数据库设计. createtableImageFiles(Idintidentity(1,1)primarykey,ImageFileimagenotnull) 这个是最简化版.问题关键是如何读取数据库的imag...

weixin_30279751的博客 312

图片上传并保存到数据库以及显示图片

图片上传并保存到数据库以及显示图片 此处是用保存图片相对路径的方法,上传图片。 1. 首先创建数据库表: create table images ( image_ID int primary key identity, image_Wpath varchar(50)not null ) --image_Wpath用来保存图片的相对路径 2. 页面: Add a ne

qq_34091529的博客 5611

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

如何将上传图片直接保存至SQL Server数据库中

这里演示的是如何将上传图片直接保存至SQL Server数据库中。 在数据库中,图片使用varbinary(MAX)存储: 这是个基于MVC3架构的例子。废话不多说,直接上代码: View: @{ ViewBag.Title = "UpLoadImg"; } @using (Html.BeginForm("Create", "UpLoadImg", FormMethod.

ying0307的专栏 1701

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

上一篇: ASP.NET会话(Session)保存模式
下一篇: 开发基于ASP.NET WebService的图片验证码服务
alys
博客等级 码龄23年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值