jQuery Ajax File Upload(附源碼)

來源:互聯網
上載者:User

項目結構

Default.aspx
Upload.aspx
Scripts/…
style.css

用戶端html代碼

隱藏行號 複製代碼 ? Default.aspx
  1. <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="UploadFile.aspx.vb" Inherits="Web.UploadFile" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title></title>
  6.     <link rel="Stylesheet" type="text/css" href="style.css" media="all" />
  7.     <script type="text/javascript" src="../Scripts/jquery-1.4.1.min.js"></script>
  8.     <script type="text/javascript" src="../Scripts/ajaxupload.3.5.js"></script>
  9.     <script type="text/javascript">
  10.         $(function () {
  11.             var btnUpload = $('#upload');
  12.             var status = $('#status');
  13.             new AjaxUpload(btnUpload, {
  14.                 action: 'Upload.aspx',
  15.                 //Name of the file input box
  16.                 name: 'uploadfile',
  17.                 onSubmit: function (file, ext) {
  18.                     if (!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))) {
  19.                         // check for valid file extension
  20.                         status.text('Only JPG, PNG or GIF files are allowed');
  21.                         return false;
  22.                     }
  23.                     status.text('Uploading...');
  24.                 },
  25.                 onComplete: function (file, response) {
  26.                     //On completion clear the status
  27.                     status.text('');
  28.                     //Add uploaded file to list
  29.                     if (response === "success") {
  30.                         $('<li></li>').appendTo('#files').html('<img src="./uploads/' + file + '" alt="" /><br />' + file).addClass('success');
  31.                     } else {
  32.                         $('<li></li>').appendTo('#files').text(file).addClass('error');
  33.                     }
  34.                 }
  35.             });
  36.         });
  37.     </script>
  38. </head>
  39. <body>
  40.     <form id="form1" runat="server">
  41.     <div id="upload">
  42.         Upload File
  43.     </div>
  44.     <!-- Upload Button-->
  45.     <div id="Div1" >Upload File</div><span id="status" ></span>
  46.     <!--List Files-->
  47.     <ul id="files" ></ul>
  48.     </form>
  49. </body>
  50. </html>

服務端處理代碼Upload.aspx

隱藏行號 複製代碼 ? Default.aspx
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. namespace JqueryAjaxUploadTest
  8. {
  9.     public partial class Upload : System.Web.UI.Page
  10.     {
  11.         protected void Page_Load(object sender, EventArgs e)
  12.         {
  13.             try
  14.             {
  15.                 HttpPostedFile hpfFile = Request.Files["uploadfile"];
  16.                 hpfFile.SaveAs(Server.MapPath("~/uploads/") + hpfFile.FileName);
  17.                 Response.Write("success");
  18.             }
  19.             catch (Exception)
  20.             {
  21.                 Response.Write("fail");
  22.             }
  23.         }
  24.     }
  25. }

:http://pjw100.download.csdn.net/
找到檔案 jquery ajax file upload(剛剛上傳,檔案可能還沒刷出來)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.