UploadFile picture uploading and ashx

Source: Internet
Author: User
Tags httpcontext random seed

UPLOADIFY.SWF needs to be supported in Chinese

$ (function () {
Get all Upload button IDs
$ ("div[id^= ' Fileinput_ ')"). each (function () {
var name = $ (this). attr ("id");
Create (name);
});
});
function Create (name) {
var select = "Upload image";
if (Name.indexof ("_") >-1) {
if (Name.substring (Name.lastindexof ("_") + 1) = = "1") {
var select = "Big picture";
}
else if (name.substring (Name.lastindexof ("_") + 1) = = "2") {
select = "Small picture";
}
}
$ (' # ' + name). uploadify ({
' Uploader ': '/uploadify/uploadify.swf ',
' Script ': ' pro_sub.aspx?upfile=1 ',
' cancelimg ': '/uploadify/cancel.png ',
' Folder ': '/productimg/',
' Fileext ': ' *.jpg;*.gif;*.png;*.bmp;*. JPEG; ',
' Filedesc ': ' Please select *.jpg;*.gif;*.png,*.bmp;*. Images in JPEG format ',
' Multi ': true,
' Auto ': true,
' ButtonText ': SELECT,
' OnComplete ': function (event, Queueid, Fileobj, response, data) {
var re = response.split (' |@| ');
if (re[0] = = "1") {
var box = name.substring (10);
var html = "<div class= ' imghtml ' onclick= ' Change (this) ' ><input class= ' input2 ' style= ' display:none; ' type= ' Button ' value= ' delete &nbsp&nbsp&nbsp except ' onclick= ' delimg (this); Showfi ("+name.replace (" _ ",". ") + "); '/><input type= ' checkbox ' checked= ' checked ' class= ' hide ' value= '" + re[1] + "'/></div>";
if (box = = "1_0") {
$ ("#ContentPlaceHolder1_rpAttribute_txtvalue_4"). Val (re[1]);
html = "<div class= ' imghtml ' onclick= ' Change (this) ' ><input class= ' input2 ' style= ' display:none; ' type= ' button ' value= ' delete &nbsp&nbsp&nbsp except ' onclick= ' delimg (this); Showfi (1) '/><input type= ' checkbox ' class= ' Hide ' value= ' + re[1] + "'/></div>";
$ ("#file0"). Hide ();
}
$ (". Box" + box). Append (HTML);
} else {
Ymprompt.errorinfo ({message: "Upload failed! ", showmask:true});
}
}
});
}

</script>

=============================================================================

Using System;
Using System.Collections.Generic;
Using System.IO;
Using System.Linq;
Using System.Web;

Using System.Web.SessionState;
Using UI;

Namespace Admin.TkAdmin.Product
{
<summary>
Summary description of Imgsave
</summary>
public class Imgsave:ihttphandler,ireadonlysessionstate
{
Public HttpRequest Request;
Public HttpResponse Response;
Public HttpSessionState Session;
public void ProcessRequest (HttpContext context)
{

Context. Response.ContentType = "Text/plain";
Response = context. Response;
Request = context. Request;
Session = context. Session;

String upfile = request["Upfile"];
if (upfile! = "")
{
If the value is 1zhi
if (Upfile = = "1")
{
UploadFile ();
}
}

}

Save the uploaded image to the specified file
private void UploadFile ()
{
Httppostedfile file = request.files["Filedata"];
String uploadfile = "";
HttpContext.Current.Server.MapPath (@Request ["folder"]);
String newfilename = "";
if (file! = null)
{
Isfile (UploadFile);
try {
Create Random Seed
Random RA;
if (session["intindeximg"]! = NULL)
{
RA = new Random (Convert.ToInt32 (session["intindeximg"));
}
Else
{
RA = new Random ();
}

int rInt = ra. Next (1000);

session["intindeximg"] = rInt;

NewFileName = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + "_" + rInt + path.getextension (file. FileName). ToUpper ();
String Bigimageurl = (Fileurl.productbigimgurl + newfilename). Replace (' \ \ ', '/');
Isfile (Fileurl.productbigimgurl);
File. SaveAs (Bigimageurl);
Save the Indent graph
String Smallimgeurl = (Fileurl.productsmallimgurl + newfilename). Replace (' \ \ ', '/');
Isfile (Fileurl.productsmallimgurl);
Createsmallpic.createimageoutput (Bigimageurl, Smallimgeurl);
Response.Write ("1|@|" + FILEURL.PRODUCTSMALLIMGURL1 + newfilename);

}catch
{
Response.Write ("0");
}
}
}

Used to create a directory
private void Isfile (String uploadfile)
{
if (! Directory.Exists (UploadFile))
{
Directory.CreateDirectory (UploadFile);
}

}

public bool IsReusable
{
Get
{
return false;
}
}
}
}

UploadFile picture uploading and ashx

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.