将图片保存到表中并用ajax技术获取图片
创建用于保存图片的表:
create table stockImages
( imageid int primary key identity(1,1),
[filename] varchar(50),
img varbinary(max))
插入本地图片:
insert into stockImages
select '风景',bulkcolumn
from openrowset(bulk 'd:\\img.jpg',single_blob) as x
创建存储过程,检索图片,将二进制数据包含在XML文档片段中:
create proc getImage @id int
as
declare @xmlResult xml
set @xmlResult=(select * from stockImages where imageid=@id for xml auto,elements,root('Items'),binary base64)
select @xmlResult
使用一般处理文件获取检索到得XML文档并发送到客户端:
<%@ WebHandler Language="C#" Class="ImageHandler" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
public class ImageHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string connectionString =
ConfigurationManager.ConnectionStrings[
"uploadConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("getImage", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.Int).Value = context.Request.QueryString["id"];
try
{
connection.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
相关文档:
AJAX无疑是2005年炒的最热的Web开发技术之一,当然,这个功劳离不开Google。我只是一个普通开发者,使用AJAX的地方不是特别多,我就简单的把我使用的心得说一下。(本文假设用户已经具有JavaScript、HTML、CSS等基本的Web开发能力)
[AJAX介绍]
Ajax是使用客户端脚本与Web服务器交换数据的Web应用开发方法。Web页 ......
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/style.css" />
<meta http-equiv="Content-Type" content="text/html; ......
内容页:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ScriptManagerProxyDemo.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script type="text/javas ......
JSON插件提供了一种名为json的ResultType,一旦为某个Action指定了一个类型为json的Result,则该Result无需映射到任何视图资源。因为JSON插件会负责将Action里的状态信息序列化成JSON格式的数据,并将该数据返回给客户端页面的JavaScript。
简单地说,JSON插件允许我们在JavaScript中异步调用Action,而且Action不再需 ......