SWFUpload plug-in + FTP File Upload, I am playing this way, swfupload File Upload

Source: Internet
Author: User
Tags ftp file

SWFUpload plug-in + FTP File Upload, I am playing this way, swfupload File Upload

:


Although I have been familiar with the swfupload upload plug-in before, what I have previously done is that this practical project is so ugly. I guess the boss's heart will crash, so I found a more elegant style on the Internet. original post address: Workshop. The front-end page is the same. After a bunch of js files are introduced, modify the Reference Path to the js file, for example: because the original version directly uses the page with garbled characters, I am too lazy to test which js file encoding does not support Chinese characters. Therefore, chartset = "gb2312" is added to all js files, and the problem is solved. Let's take a look at the configuration in js. The changes are as follows: upload_url: the address of the request to upload. Request self-written aspx, ashx, or MVC controllerfile_size_limit: limit the size of the uploaded file. Of course, this is only the plug-in's initial detection of the file size. To prevent large request packet attacks, iis has already imposed restrictions on File Upload by default. I forgot the specific data, the test is within 5 MB, so it is not possible to upload as much as it is written here. To modify the file upload size, see file_types: Check the file type. You can also configure the following file_upload_limit to configure the number of uploaded files in the background code. Upload FTP and insert the database according to the project:
/// <Summary> /// file Upload /// </summary> /// <returns> </returns> public ActionResult FileUpload () {HttpPostedFileBase file = Request. files ["Filedata"]; string fileName = Path. getFileName (file. fileName); string fileExt = Path. getExtension (fileName); int fileSize = file. contentLength; string msg = string. empty; if (fileExt = ". jpg "| fileExt = ". gif "| fileExt = ". flv "| fileExt = ". mp4 ") {// obtain the FTP address from the database string serverIP = NVP_FTPServerService.LoadData (" DepartmentID ", LoginUser. specified mentid. toString ()). firstOrDefault (). FTPServer; string userName = System. configuration. configurationManager. appSettings ["FTPUserName"]; string password = System. configuration. configurationManager. appSettings ["FTPPassword"]; string dir = "/UploadFile/" + DateTime. now. toString () + "/" + fileName; string localPath = Request. mapPath (dir); Directory. createDirectory (Path. getDirectoryName (localPath); file. saveAs (localPath); // upload Ftp FtpStatusCode ftpCode = FtpUpload (localPath, null, serverIP, userName, password); // Delete the original file System. IO. file. delete (localPath); if (ftpCode = FtpStatusCode. closingData) {// enter the database int count = SaveDataInfo (fileSize, fileName); if (count> 0) {msg = "uploaded" ;}else {msg = "database error, unable to display the uploaded file information ";}} else {msg =" FTP server error ";}} else {msg =" Upload file type error ";}return Content (msg );}

FTP upload part

Private FtpStatusCode FtpUpload (string delimiter, string FolderName, string ftpServerIP, string ftpUserName, string ftpPwd) {try {FileInfo fileInf = new FileInfo (plaintext); FtpWebRequest reqFTP; FtpWebResponse uploadResponse = null; reqFTP = (FtpWebRequest) FtpWebRequest. create (new Uri (ftpServerIP + "/" + FolderName + "/" + fileInf. name); reqFTP. credentials = new NetworkCredential (ftpUserN Ame, ftpPwd); reqFTP. keepAlive = false; reqFTP. method = WebRequestMethods. ftp. uploadFile; reqFTP. useBinary = true; reqFTP. contentLength = fileInf. length; int buffLength = 2048; byte [] buff = new byte [buffLength]; int contentLen; using (FileStream fs = fileInf. openRead () {using (Stream strm = reqFTP. getRequestStream () {contentLen = fs. read (buff, 0, buffLength); while (contentLen! = 0) {strm. write (buff, 0, contentLen); contentLen = fs. read (buff, 0, buffLength) ;}} uploadResponse = (FtpWebResponse) reqFTP. getResponse (); return uploadResponse. statusCode;} catch (Exception e) {return FtpStatusCode. undefined; throw new Exception ("Ftp error", e );}}
OK, the basic configuration has been completed. However, due to the limitations imposed by iis mentioned above, we still need to remove this operation. 1. in the Web. add the httpRuntime node to the config file Function fileDialogComplete1 (numFilesSelected, numFilesQueued) {try {var info = $ ('# info '). val (); if (numFilesQueued> 0) {if (info = "") {alert ("information cannot be blank"); swfu. cancelUpload (); return;} // Add a custom parameter to the Upload File for receiving and processing var postParams = {'info': info}; swfu in the background. setPostParams (postParams); if (numFilesSelected> 0) {document. getElementById (this. customSettings. cancelButtonId ). disabled = false;} swfu. startUpload () ;}} catch (ex) {this. debug (ex );}}

In fact, there are many events that SWFupload can play. You can see in the handler. js file that you can directly modify if you need to control an event. For details, see this article:

SWFUpload 2.5.0 official documentation http://www.cnblogs.com/youring2/archive/2012/07/13/2590010.html

Related Article

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.