ASP. Upload and save a description of each image

Source: Internet
Author: User

Foreground aspx

Picture Previews and descriptions
function Previewimage (file) {
var div = document.getElementById (' preview ');
div.innerhtml = "";
for (var i = 0; i < file.files.length; i++) {
Alert (File.files[i]);
var ndiv = document.createelement ("div");
Ndiv.style.height = "150px";
Ndiv.style.width = "300px";
Ndiv.style.cssFloat = "Left";
var img = document.createelement ("img");
var TextArea = document.createelement ("TextArea");
TextArea.style.width = "100";
TextArea.style.height = "60px";
Textarea.setattribute ("name", "note" +i);//a name for the control that fills in the notes, and the number of pictures associated
Ndiv.appendchild (IMG);
Ndiv.appendchild (TextArea);
img.id = "img" + i;
Div.appendchild (NDIV);
Img.width = 200;
Img.height = 200;

}
for (var i = 0; i < file.files.length; i++) {
var pic = document.getElementById ("img" + i);
pic.src = window. Url.createobjecturl (File.files[i]);

}

}

<form id= "Form1" method= "POST" runat= "server" enctype= "Multipart/form-data";
<div style= "Text-align: Center "
<div>
<input type=" file "multiple=" multiple "onchange=" Previewimage (This) "id=" MyImage "runat=" Server "class=" btn Btn-default "style=" margin-left:40%; height:69px; width:280px "/>
<p id=" MyFile ";
<asp:button runat=" Server "text=" Confirm return "onclick=" Unnamed_click " cssclass= "btn btn-default" height= "68px" width= "124px"/>
&nbsp;&nbsp;
<asp:button runat= " Server "text=" starts uploading "id=" Uploadbutton "onclick=" Uploadbutton_click "cssclass=" btn Btn-default "height=" 69px "width=" 129px "></ASP:BUTTON>
</p>
<p>
<asp:label id=" strstatus "runat=" Server " font-names= "Arial" font-bold= "True" font-size= "9pt" width= "500px"
borderstyle= "None" bordercolor= "white" ></ Asp:label>
</p>
</div>

<%--Picture Preview--%>
<div id= "Preview" >
</div>
</div>
</form>

Backstage CS

private void Saveimages ()
{

Traversing a file Form element
httpfilecollection files = HttpContext.Current.Request.Files;

Status information
System.Text.StringBuilder STRMSG = new System.Text.StringBuilder ();
Strmsg.append ("uploaded files are: Try
{
for (int iFile = 0; iFile < files. Count; ifile++)
{
String note= request["Note" +ifile];//get a description of each picture
String fileName = Files[ifile]. FileName;
String path = Server.MapPath ("~/upload/");
String imgtype = Files[ifile]. Contenttype.tostring (); Type of picture

if (fileName! = "" && (Imgtype.equals ("Image/bmp") | | Imgtype.equals ("image/jpg") | | Imgtype.equals ("Image/jpeg") | | Imgtype.equals ("Image/gif") | | Imgtype.equals ("Image/png")))
{

String uppath = Filecontroller.getuppath ();
filename = IFile + "_" + filename;
String path2 = Server.MapPath ("~/upload/");
Generate artwork
byte[] Ofilebyte = new Byte[files[ifile]. ContentLength];
System.IO.Stream OStream = Files[ifile]. InputStream;
System.Drawing.Image oimage = System.Drawing.Image.FromStream (OStream);

int owidth = Oimage.width; Original width
int oheight = Oimage.height; Original height

int twidth = 100; Set thumbnail initial width
int theight = 100; Set thumbnail initial height

int add1 = 20;
int ADD2 = 120;

Graphics g = null;
Bitmap timage = null;

String fileextension = System.IO.Path.GetExtension (fileName);
Strmsg.append ("uploaded file type:" + files[ifile]. Contenttype.tostring () + "<br>");
Strmsg.append ("Client file Address:" + files[ifile]. FileName + "<br>");
Strmsg.append ("FileName of uploaded file:" + filename + "<br>");
Strmsg.append ("uploaded file extension:" + fileextension + "<br>for (int i = 0; i < 4; i++)
{
Calculates the width and height of the thumbnail proportionally
if (owidth >= oheight)
{
Theight = (int) Math.floor (convert.todouble (oheight) * (convert.todouble (twidth)/convert.todouble (oWidth)));
}
Else
{
Twidth = (int) Math.floor (convert.todouble (owidth) * (convert.todouble (theight)/convert.todouble (oHeight)));
}
Generate thumbnail artwork
Timage = new Bitmap (twidth, theight);
g = Graphics.fromimage (timage);
G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High; Setting high-quality interpolation methods
G.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;//Set high quality, low speed rendering smoothness
G.clear (color.transparent); Empty the canvas and fill it with a transparent background color
G.drawimage (Oimage, New Rectangle (0, 0, Twidth, theight), new Rectangle (0, 0, Owidth, oheight), GraphicsUnit.Pixel);
String savePath2 = Path + Files[ifile]. FileName;
String Savepath = "~/upload/" + "(" + Twidth + "-" + Theight + ")" + fileName;
Try
{
Save a picture in JPG format
Oimage.save (Ofullname, System.Drawing.Imaging.ImageFormat.Jpeg);
Timage.save (path2 + "(" + Twidth + "-" + Theight + ")" + FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
Save picture Path to database
Inv_worklistinfo inv_wlinfo = new Inv_worklistinfo ();
Inv_wlinfo.picurl = Savepath;
Inv_wlinfo.workid = WorkID;
Inv_wlinfo.notes = Note;
Inv_worklistfactory.create (). ADD (Inv_wlinfo);
}
catch (Exception ex)
{
Strstatus.text = ex. Message;
}
if (i = = 0)
{
Twidth = + add1; Set thumbnail initial width
Theight = + add1; Set thumbnail initial height
}
if (i = = 1)
{
Twidth = + add2; Set thumbnail initial width
Theight = + add2; Set thumbnail initial height
}
if (i = = 2)
{
Twidth = Owidth; Set thumbnail initial width
Theight = Oheight; Set thumbnail initial height
}
}
Freeing resources
if (! Cmputil.isnulloemp (Oimage))
{
Oimage.dispose ();
}
if (! Cmputil.isnulloemp (g))
{
G.dispose ();
}
if (! Cmputil.isnulloemp (Timage))
{
Timage.dispose ();
}
}
Else
{
Jscontroller.alert (this, "image format only supports: Jpeg,jpg,png,bmp,gif");
}
}
Strstatus.text = Strmsg.tostring ();
}
catch (System.Exception Ex)
{
Strstatus.text = Ex.message;
}
}

}

ASP. Upload and save a description of each image

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.