Trang chủĐăng kýĐăng nhập Cộng đồng tin học
Thứ 2, 2024-04-29, 3:49 AM
Khung đăng nhập

Khung tán gẫu
Xóm 'bà Tám'

http://congdongtinhoc.net
CHUYÊN TRANG GAME ONLINE GIẢI TRÍ//lehung-system.ucoz.net/stuff/

Thống kê diễn đàn
Bài viết mới nhất Trang chủ cập nhật Top 10 thành viên tích cực 10 Thành viên mới nhất
  • Quạt Hướng Trục
  • vào ucoz.com thiết kế web không hiểu sao...
  • Sothink DHTMLMenu 9.2 Build 90326
  • cho em quảng cáo cái
  • BIDV triển khai gói 10.000 tỷ đồng cho v...
  • Phượng Đã Nở Ngoài Hiên
  • Ngựa Ô Thương Nhớ
  • Những mẩu chuyện vui
  • 1001 cách biến "sim rác" thành...
  • Windows XP Media Center Edition 2008 - S...
  • Hướng dẫn chỉnh sửa dữ liệu trong form m...
  • 15 điều người dùng máy tính nên biết
  • Choáng vì "sâu" mới phát tán qua email
  • Giấu bớt những thành phần Control Panel ...
  • Thảo luận về IFrame Injection Attacks
  • Miễn phí bản quyền Ashampoo Anti-Malware...
  • Trải nghiệm với Camtasia Studio 7
  • 10 kỹ năng IT ‘hot’ của năm 2011
  • Intel công bố bộ vi xử lý Hệ thống trên ...
  • Kho phần mềm dành cho Android
  • Hung@info
  • thangbom
  • Hung@webmater
  • hebeo
  • giodaingan
  • david15
  • whitecat
  • luutruthongtin
  • systemfan_12
  • sha66b5cates0428
  • amir2x4
  • taiwindows075
  • kholuutru
  • shahmeerolivedigital9
  • ysg06363100
  • hetoxe6474
  • rootanalysisusa
  • memory_gift
  • systemfan_12
  • quatcongnghiep_saigon


  • [ Tổng hợp bài mới · Tổng số thành viên · Nội qui chung · Tìm kiếm bài viết · RSS ]
    • Page 1 of 1
    • 1
    Diễn đàn » Lập Trình » .NET » Ajax và asp.net upload file ( không xài dll hổ trợ)
    Ajax và asp.net upload file ( không xài dll hổ trợ)
    Hung@infoDate: Thứ 2, 2009-05-25, 10:37 PM | Bài viết # 1
    Trung úy
    Nhóm: Quản trị viên
    Bài viết: 994
    Uy tín: 10
    Hiện tại: Offline
    Để upload được dữ liệu từ client bạn cần phải dùng hàm javascript để lấy dữ liệu trong thư mục client mà truyền lên cho sever. và sau đó sever sẽ dùng hàm Reuqest.Files để nhận chuỗi dữ liệu file truyền từ client. Bên cạnh đó các bạn cũng nhớ đến đặt thêm thuộc tính

    BUG : Dữ liệu nhận được không giống với dữliệu ban đầu...không biết format thế nào mới đúng

    <form runat="server" id="from" enctype="multipart/form-data" > để đảm bảo không sai xót

    Code
    HTML File
    --------------------------------
    <script language="javascript">
    var url = "ajax/ajaxFile.aspx";
    var binary;
    var filename;
    var mytext;

    function upload() {
         filename = document.getElementById('myfile').value;
         mytext = document.getElementById('mytext').value;
         document.getElementById('ajaxbutton').disabled = true;

         // request local file read permission
         try {
             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
         } catch (e) {
             alert("Permission to read file was denied.");
         }
           
         // open the local file
         var file = Components.classes["@mozilla.org/file/local;1"]
             .createInstance(Components.interfaces.nsILocalFile);
         file.initWithPath( filename );          
         stream = Components.classes["@mozilla.org/network/file-input-stream;1"]
             .createInstance(Components.interfaces.nsIFileInputStream);
         stream.init(file,    0x01, 00004, null);
         var bstream =  Components.classes["@mozilla.org/network/buffered-input-stream;1"]
             .getService();
         bstream.QueryInterface(Components.interfaces.nsIBufferedInputStream);
         bstream.init(stream, 1000);
         bstream.QueryInterface(Components.interfaces.nsIInputStream);
         binary = Components.classes["@mozilla.org/binaryinputstream;1"]
             .createInstance(Components.interfaces.nsIBinaryInputStream);
         binary.setInputStream (stream);

         // start AJAX file upload in 1 second
         window.setTimeout("ajax_upload()", 1000);
    }

    function ajax_upload() {
         // request more permissions
         try {
             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
         } catch (e) {
             alert("Permission to read file was denied.");
         }

         http_request = false;
         http_request = new XMLHttpRequest();
         if (!http_request) {
             alert('Cannot create XMLHTTP instance');
             return false;
         }

         // prepare the MIME POST data
         var boundaryString = 'capitano';
         var boundary = '--' + boundaryString;
         var requestbody = boundary + '\n'
         + 'Content-Disposition: form-data; name="mytext"' + '\n'
         + '\n'
         + mytext + '\n'
         + '\n'
         + boundary + '\n'
         + 'Content-Disposition: form-data; name="myfile"; filename="'
             + filename + '"' + '\n'
         + 'Content-Type: application/octet-stream' + '\n'
         + '\n'
         + escape(binary.readBytes(binary.available()))
         + '\n'
         + boundary;

         document.getElementById('sizespan').innerHTML =
             "requestbody.length=" + requestbody.length;
           
         // do the AJAX request
         http_request.onreadystatechange = requestdone;
         http_request.open('POST', url, true);
         http_request.setRequestHeader("Content-type", "multipart/form-data; \
             boundary=\"" + boundaryString + "\"");
         http_request.setRequestHeader("Connection", "close");
         http_request.setRequestHeader("Content-length", requestbody.length);
         http_request.send(requestbody);

    }

    function requestdone() {
         if (http_request.readyState == 4) {
             if (http_request.status == 200) {
                 result = http_request.responseText;
                 document.getElementById('myspan').innerHTML = result;              
             } else {
                 alert('There was a problem with the request.');
             }
             document.getElementById('ajaxbutton').disabled = false;
         }
    }

    </script>

    <form>
    Text: <input type="text" id="mytext" name="mytext" size="40">
    <br>
    File: <input type="file" id="myfile" name="datafile" size="40"><br>
    <input type="button" id="ajaxbutton" value="AJAX IT" onclick="upload();">
    </form>

    <div id="sizespan"></div>
    <hr>
    <div id="myspan"></div>
    --------------------------------------------------------------
    asp.net File
    protected void Page_Load(object sender, EventArgs e)
         {
             if (Request.Files.Count > 0)
             {
                 try
                 {
                     for (int i = 0; i < Request.Files.Count; i++)
                     {

                         int values = Request.Files.FileName.LastIndexOf('\\');
                         string filename = Request.Files.FileName.Substring(values + 1);
                         String strSavePath = Server.MapPath(ConfigurationManager.AppSettings["NewsImage"]) +"\\"+ filename;
                         Response.Write(strSavePath);
                         Request.Files.SaveAs(strSavePath);
                     }
                 }
                 catch(Exception objEx)
                 {
                     Response.Write(objEx.Message);
                 }
             }
             else
             {
                 Response.Write(" khong co file");
             }
         }


    --== Cộng đồng tin học ==--
     
    Diễn đàn » Lập Trình » .NET » Ajax và asp.net upload file ( không xài dll hổ trợ)
    • Page 1 of 1
    • 1
    Search:


      Copyright Cộng đồng tin học © 2024