Last active
September 23, 2015 22:11
-
-
Save Implem/dab47176b09356ea51db to your computer and use it in GitHub Desktop.
c# テキストエリア上でペーストした画像をSQL Serverに保存する ref: http://qiita.com/Implem/items/9c9bdaffa65ce102d064
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contentType: false, | |
processData: false, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dataType: 'json', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var file = HttpContext.Current.Request.Files[key]; | |
var bin = new byte[file.ContentLength]; | |
file.InputStream.Read(bin, 0, file.ContentLength); | |
return bin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return new FileContentResult(image, "image/jpeg"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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