Image upload function demo, using HTML5 's Clipboard API interface

Source: Internet
Author: User
Tags allkeys

<%@ page language= "C #" autoeventwireup= "true" codebehind= "WebForm1.aspx.cs" inherits= "Testapi.webform1"%>


<title>test</title>

<style>

div#editable {

width:400px;

height:300px;

border:1px dashed blue;

}

</style>

<script type= "Text/javascript" >


Window.onload = function () {

function Paste_img (e) {

if (E.clipboarddata.items) {

Google-chrome

Alert (' Support Clipboarddata.items (chrome ...) ');

Ele = E.clipboarddata.items

for (var i = 0; i < ele.length; ++i) {

if (Ele[i].kind = = ' file ' && ele[i].type.indexof (' image/')!==-1) {

var blob = Ele[i].getasfile ();

Console.log (Bloburl);


Readblobasdataurl (blob, function (URL) {


var len = document.getElementById ("editable"). getElementsByTagName ("img"). length + 1;


var new_img = document.createelement (' img ');

New_img.setattribute (' name ', ' Imgpic ');

New_img.setattribute (' id ', ' imgpic ' + len);

New_img.setattribute (' src ', url);

New_img.setattribute ("width", 600);

New_img.setattribute ("height", 450);


document.getElementById (' editable '). appendchild (NEW_IMG);

});

}

}

} else {

Alert (' Non-chrome ');

}

}

document.getElementById (' editable '). Onpaste = function () {paste_img (event); return false;};

}


function Save ()

{

var omyform = new FormData ();

Omyform.append ("username", "Groucho");

Omyform.append ("AccountNum", 123456);


Alert (document.getElementById ("Imgpic"). getattribute (' src '));


The files selected by the user are already included in the Fileinputelement

Omyform.append ("UserFile", document.getElementById ("Imgpic"). getattribute (' src '));

var ofilebody = "<a id=\" a\ "><b id=\" b\ ">hey!</b></a>"; Blob object contains the file contents


for (var i = 1; I <= document.getElementById ("editable"). getElementsByTagName ("img"). Length; i++) {

var Oblob = Dataurltoblob (document.getElementById ("Imgpic" +i). getattribute (' src ')); New Blob ([Ofilebody], {type: "Text/xml"});

Omyform.append ("Webmasterfile", Oblob);

}


var oreq = new XMLHttpRequest ();

Oreq.open ("POST", "http://localhost:42516/WebForm1.aspx");

Oreq.send (Omyform);

}


function Dataurltoblob (dataurl) {

var arr = dataurl.split (', ');

var mime = arr[0].match (/:(. *?);/) [1],

BSTR = Atob (arr[1]), n = bstr.length, U8arr = new Uint8array (n);

while (n--) {

U8arr[n] = bstr.charcodeat (n);

}

return new Blob ([U8arr], {type:mime});

}


function Readblobasdataurl (BLOB, callback) {

var a = new FileReader ();

A.onload = function (e) {callback (e.target.result);};

A.readasdataurl (BLOB);

}


</script>

<body >

<input id= "btnclear" type= "button" onclick= "document.getElementById (' editable '). InnerHTML =" "value=" Empty Picture "/>

<input id= "btnsave" type= "button" value= "Upload picture"/>

<div id= "Editable" contenteditable= "true" ></div>

</body>






Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Web;

Using System.Web.UI;

Using System.Web.UI.WebControls;


Namespace Testapi

{

public partial class WebForm1:System.Web.UI.Page

{

protected void Page_Load (object sender, EventArgs e)

{

string[] array = Request.Form.AllKeys;

if (array. Length > 0)

{

Httppostedfile postfilebase;

if (Request.Files.Count > 0)

{

String Dirpath = this. Server.MapPath ("~/1");

If the folder does not exist, create

Filehandle.createdir (Dirpath);

Result. FileCount = Request.Files.Count;

Result. FilePath = new String[result. FileCount];

String Filenamepath = String. Empty;

for (int i = 0; i < Request.Files.Count; i++)

{

Postfilebase = Request.files[request.files.allkeys[i]];

if (postfilebase! = null)

{

if (PostFileBase.ContentType.StartsWith ("image"))

{

Postfilebase.saveas (String. Format ("{0}\\{1}.png", Dirpath, DateTime.Now.Ticks.ToString ()));

}

else if (PostFileBase.ContentType.StartsWith ("text"))

{

Postfilebase.saveas (String. Format ("{0}\\{1}.txt", Dirpath, DateTime.Now.Ticks.ToString ()));

}

}

}

}

}

}

}

}


Image upload function demo, using HTML5 's Clipboard API interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.