Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of ASP.NET File upload Control Uploadify

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the usage of ASP.NET file upload control Uploadify". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "ASP.NET file upload control Uploadify usage" it!

Introduction to related API

Uploader: the relative path to the uploadify.swf file, which is a button with the text BROWSE. Click and fade out to open the file dialog box. Default: uploadify.swf.

Script: the relative path of the background processor. Default value: uploadify.php

CheckScript: the relative path of the background processor used to determine whether the file selected for upload exists on the server

FileDataName: set a name to fetch the data of the uploaded file in the server handler. Default is Filedata

Method: submission method Post or Get defaults to Post

ScriptAccess: the access mode of the flash script file. If the local test is set to always, the default value is sameDomain

Folder: the directory where the uploaded files are stored.

QueueID: the ID of the file queue, which is the same as the ID of the div where the file queue is stored.

QueueSizeLimit: set the number of selected files when many files are allowed to be generated. The default value is 999.

Multi: multiple files can be uploaded when set to true.

Auto: set to true when the file is selected and uploaded directly. For false, you need to click the upload button to upload.

FileExt: sets the type of file that can be selected, such as:'* .jpg;*.gif,*.png'.

FileDesc: this property value is valid only after setting the fileExt property. It is used to set the prompt text in the select file dialog box, such as setting fileDesc to "Please select an image file".

SizeLimit: size limit for uploaded files.

SimUploadLimit: the number of simultaneous uploads allowed. Default value: 1.

ButtonText: text of the browse button. Default value: BROWSE.

ButtonImg: the path to the picture of the browse button.

HideButton: set to true to hide the picture of the browse button.

Rollover: the values are true and false, and when set to true, it reverses when the mouse moves over the browse button.

Width: sets the width of the browse button. The default value is 110.

Height: sets the height of the browse button. The default is 30.

Wmode: setting this item to transparent makes the flash background file of the browse button transparent, and the flash file is set to the highest level of the page. Default value: opaque.

CancelImg: select the close button icon on each file after the file is transferred to the file queue

Structure diagram

HTML code

Upload | cancel upload

JS code

$("# custom_file_uploadEdu"). Uploadify ({'uploader':' / Scripts/Uploadify/uploadify.swf', 'script':' / ashx/UploadFile.ashx', 'cancelImg':' / Scripts/Uploadify/uploadify-cancel.png', 'folder':' /', 'queueSizeLimit': 1,' simUploadLimit': 1, 'sizeLimit': 1024 * 1024 * 5, 'multi': false,' auto': false,/* if automatic upload Then the upload button will be useless * / 'fileExt':' * .jpg * .gif;*.jpeg;*.mp4', 'fileDesc':' Please select Image or Video', 'queueID':' fileQueue', 'width': 110,' height': 30, 'buttonText':' Select', 'wmode':' opaque', 'hideButton': false,' onSelect': function (event, ID, fileObj) {$("# displayMsg"). Html ("uploading.") }, 'onComplete': function (event, queueId, fileObj, response, data) {var ary = response.split (' |'); if (ary [0] = = "0") {/ / error message alert (ary [1]);} else {if (ary [0] = = "1") {/ / URL $("# displayMsg"). Html ("upload successful") $("# ImagePath"). Attr ("value", ary [1]) $("# ImagePath"). Remove ("img"). Next ("img"). Show (). Attr ({"style": "width:50px;height:50px;", "src": ary [1]});} else {/ / exception information alert (ary [1]);}})

Background handler (receive stream, write stream)

Summary description of namespace WebTest.ashx {/ UploadFile / / public class UploadFile: IHttpHandler {public void ProcessRequest (HttpContext context) {context.Response.ContentType = "text/plain"; context.Response.Write (new UploadImpl (). Upload (context, UpLoadType.ProductImage, false));} public bool IsReusable {get {return false;}}

UploadImpl class code

Implementation of namespace EntityFrameworks.Application.Core.FileUpload {/ Image upload function / public class UploadImpl {public UploadImpl (IFileUploadSize fileUploadSize) {_ fileUploadSize = fileUploadSize?? New TestFileUploadSize ();} public UploadImpl (): this (null) {} # region Fields & Consts static string FileHostUri = System.Configuration.ConfigurationManager.AppSettings ["FileHostUri"]? HttpContext.Current.Request.Url.Scheme + ": / /" + HttpContext.Current.Request.Url.Authority; Point point = new Point (0,0); / / the image is intercepted from that coordinate point double wRate = 1, hRate = 1, setRate = 1; int newWidth = 0, newHeight = 0; IFileUploadSize _ fileUploadSize # endregion # region Image zooming / zooming / zooming file / / width / height / whether the address to be stored after scaling / bool CreateThumbnail (HttpPostedFile file, ImageSize imageSize, bool isEqualScale, string name) {double width = (double) imageSize.Width; double height = (double) imageSize.Height; Try {System.Drawing.Image image = System.Drawing.Image.FromStream (file.InputStream); if (isEqualScale) {if (image.Height > height) {hRate = height / image.Height;} if (image.Width > width) {wRate = width / image.Width;} if (wRate! = 1 | hRate! = 1) {if (wRate > hRate) {setRate = hRate;} else {setRate = wRate } newWidth = (int) (image.Width * setRate); newHeight = (int) (image.Height * setRate); if (height > newHeight) {point.Y = Convert.ToInt32 (height / 2-newHeight / 2);} if (width > newWidth) {point.X = Convert.ToInt32 (width / 2-newWidth / 2);} Bitmap bit = new Bitmap (int) (width), (int) (height)) Rectangle r = new Rectangle (point.X, point.Y, (int) (image.Width * setRate), (int) (image.Height * setRate)); Graphics g = Graphics.FromImage (bit); g.Clear (Color.White); g.DrawImage (image, r); MemoryStream ms = new MemoryStream (); bit.Save (ms, ImageFormat.Jpeg); byte [] bytes = ms.ToArray (); string fileName = name + imageSize.ToString () / rename using (FileStream stream = new FileStream (fileName, FileMode.Create, FileAccess.Write)) {stream.Write (bytes, 0, bytes.Length);} bit.Dispose (); ms.Dispose (); image.Dispose (); return true;} catch (Exception) {return false The proportional scaling of the / bool CreateThumbnail (HttpPostedFile file, ImageSize imageSize, string name) {return CreateThumbnail (file, imageSize, true, name);} # endregion public string Upload (HttpContext context, UpLoadType type, bool isScale) {ImageSize imageSize = _ fileUploadSize.ImageSizeForType [type]; HttpFileCollection files = context.Request.Files If (files.Count = = 0) {throw new ArgumentNullException ("please choose file for upload.");} string path = "/ upload/" + type.ToString (); / / relative path if (! Directory.Exists (path)) Directory.CreateDirectory (path); / / only the 1st file var file = files [0] If (file! = null & & file.ContentLength > 0) {try {string filename = context.Request.Form ["fileName"] .split ('.') [0] + "_" + DateTime.Now.ToString ("yyyyMMddhhssmm") + imageSize.ToString (); / / Local file system path string savePath = Path.Combine (context.Server.MapPath (path), filename); file.SaveAs (savePath); if (isScale) CreateThumbnail (file, imageSize, savePath) / / return the URI path string ImageUri = FileHostUri + path + "/" + filename; return "1 |" + ImageUri;} catch (Exception ex) {return "0 |" + ex.Message;}} return null;}

Effect picture:

At this point, I believe that everyone on the "ASP.NET file upload control Uploadify usage" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report