Skip to content

Instantly share code, notes, and snippets.

@Implem
Last active September 23, 2015 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Implem/dab47176b09356ea51db to your computer and use it in GitHub Desktop.
Save Implem/dab47176b09356ea51db to your computer and use it in GitHub Desktop.
c# テキストエリア上でペーストした画像をSQL Serverに保存する ref: http://qiita.com/Implem/items/9c9bdaffa65ce102d064
CREATE DATABASE [TestDB]
GO
USE [TestDB];
CREATE TABLE [dbo].[Images](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Bin] [image] NOT NULL,
CONSTRAINT [PK_Images] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
contentType: false,
processData: false,
// dataType: 'json',
var file = HttpContext.Current.Request.Files[key];
var bin = new byte[file.ContentLength];
file.InputStream.Read(bin, 0, file.ContentLength);
return bin;
return new FileContentResult(image, "image/jpeg");
using System.Data;
using System.Data.SqlClient;
using System.Web;
namespace WebApplication1.Models
{
public class ImageModel
{
const string ConnectionString = "Server=tcp:(local),1433;Database=TestDB;Integrated Security=SSPI;";
public long Id;
public ImageModel()
{
}
public ImageModel(long id)
{
Id = id;
}
public byte[] Show()
{
var dt = new DataTable();
var conn = new SqlConnection(ConnectionString);
var cmd = new SqlCommand("select [Images].[Bin] from [Images] where [Images].[id]=@Id;", conn);
cmd.Parameters.AddWithValue("@Id", Id);
var adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
return dt.Rows[0][0] as byte[];
}
public long Create()
{
var conn = new SqlConnection(ConnectionString);
var cmd = new SqlCommand("insert into [Images]([Images].[Bin]) values(@Bin); select @@identity;", conn);
cmd.Parameters.AddWithValue("@Bin", File("Images_Bin"));
conn.Open();
long.TryParse(cmd.ExecuteScalar().ToString(), out Id);
conn.Close();
return Id;
}
public static byte[] File(string key)
{
var file = HttpContext.Current.Request.Files[key];
var bin = new byte[file.ContentLength];
file.InputStream.Read(bin, 0, file.ContentLength);
return bin;
}
}
}
using System.Web.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
[AllowAnonymous]
public class ImagesController : Controller
{
[HttpGet]
public ActionResult Show(long id)
{
var image = new ImageModel(id).Show();
return new FileContentResult(image, "image/jpeg");
}
[HttpPost]
public string Create()
{
var id = new ImageModel().Create();
return id.ToString();
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.min.js"></script>
<script src="Scripts/test.js"></script>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<textarea id="Images_Bin" class="upload-image" rows="5" placeholder="ここに画像をペースト"></textarea>
<div id="results" style="margin-top:10px;"></div>
</body>
</html>
$(document).on('paste', '.upload-image', function (e) {
var items = e.originalEvent.clipboardData.items;
for (var i = 0 ; i < items.length ; i++) {
var item = items[i];
if (item.type.indexOf("image") != -1) {
var data = new FormData();
data.append('Images_Bin', item.getAsFile());
requestFile('/images/create', 'post', data);
}
}
});
function requestFile(requestUrl, methodType, data) {
return $.ajax({
url: requestUrl,
type: methodType,
cache: false,
contentType: false,
processData: false,
data: data,
success: function (o) {
$('#results').prepend('<img src="/images/show/' + o + '">');
return true;
},
error: function (xhr, status, err) {
return false;
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment