Asp.net+ajaxfileupload.js implementation of file asynchronous upload code sharing _ Practical skills

Source: Internet
Author: User

Because the code is very simple, there is no more gossip, directly on the code, the small partners themselves read the code to understand.

Foreground code:

Copy Code code as follows:

* * Change Avatar/*
Upload
function _sc () {
$ (". Ckfile"). HTML (""). CSS ("Color", "#535353");
$ ("#_userImgPath"). Val ("");
var str = $ ("#file"). Val ();
if ($.trim (str) = "") {
$ (". Ckfile"). HTML ("Please select a file.") "). CSS (" Color "," red ");
return false;
}
else {
var postfix = str.substring (Str.lastindexof (".") + 1). toUpperCase ();
if (Postfix = = "JPG" | | postfix = = "JPEG" | | | postfix = "PNG" | | postfix = = "GIF" | | postfix = = "BMP") {
$ (' #showimg '). attr (' src ', ' images/loading.gif '). attr ("title", "Crosses, please later ...");
var path = "Upload/userimg";
$.ajaxfileupload ({
URL: '/upload.aspx?path=upload| Userimg&shape=100*100 ',
Secureuri:false,
Fileelementid: ' File ',
DataType: ' Text ',
Success:function (msg) {
if (msg.lastindexof (path) = = 1) {
$ (". Ckfile"). HTML (msg). CSS ("Color", "red");
}
else {
$ (' #showimg '). attr (' src ', msg). attr ("title", "My Avatar");
$ ("#_userImgPath"). Val (msg);
}
}
});
} else {
$ (". Ckfile"). HTML ("file format error.") "). CSS (" Color "," red ");
return false;
}
}
}

Background code:

Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using SS. Upload;
Using WFC. Fenxiao;
Namespace Wanfangcheng
{
public partial class Upload:basepage
{
File Size 1024 kb
Private long size = 1024;
File type
private String type = ". Jpg|. Jpeg|. Png|. Gif|. BMP ";
Save Name
String name = "";
Save path
private string Path = @ "Upload/userimg";
Save Size
private string shape = "100*100";
protected void Page_Load (object sender, EventArgs e)
{
httpfilecollection files = request.files;
if (Files!= null && files. Count > 0)
{
Name = BaseRole.Instance.UserId.ToString ();
if (request.querystring["size"]!= null)
{
Size = Convert.ToInt32 (request.querystring["size"]);
}
if (request.querystring["path"]!= null)
{
Path = request.querystring["path"]. ToString (). Trim (). Replace (' | ', '/');
}
if (request.querystring["name"]!= null)
{
Name = request.querystring["Name"]. ToString (). Trim ();
}
if (request.querystring["shape"]!= null)
{
Shape = request.querystring["shape"]. ToString (). Trim ();
}
Uploadmethod (files);
}
}
<summary>
Upload pictures
</summary>
<param name= "HC" ></param>
public void Uploadmethod (Httpfilecollection hc)
{
Httppostedfile _file = hc[0];
File size
Long _size = _file. ContentLength;
if (_size <= 0)
{
Response.Write ("File error.") ");
Response.End ();
Return
}
if (Size * 1024 < _size)
{
Response.Write ("The file is too large, the maximum limit is + size +" KB.) ");
Response.End ();
Return
}
Filename
string _name = _file. FileName;
File format
String _tp = System.IO.Path.GetExtension (_name). ToLower ();
if (type. IndexOf (_TP) = =-1)
{
Response.Write ("File format error.") ");
Response.End ();
Return
}
Save path
String _path = HttpContext.Current.Server.MapPath (path) + @ "/" + name + _tp;
Try
{
int w = Convert.ToInt32 (shape. Split (' * ') [0]);
int h = Convert.ToInt32 (shape. Split (' * ') [1]);
Imagehelper.cutforcustom (_file, _path, W, H, 50);
Response.Write (Path + @ "/" + name + _TP);
}
catch (Exception)
{
Response.Write ("Oops, there was a mistake.") ");
Response.End ();
}
}
}
}

is not very practical, but also very easy to understand, the above is the code used in their own projects, small partners if the problem is found in the place, but also please tell. Thank you

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.