javascript sending file problem when recive

Last post 07-06-2008 11:44 AM by hoaiduc2304. 4 replies.

Sort Posts:

  • javascript sending file problem when recive

    06-28-2008, 2:50 AM
    • Member
      1 point Member
    • hoaiduc2304
    • Member since 05-24-2007, 4:34 AM
    • Posts 6

     hi bro,

    i dont know how to convert the file when i am using the javascript send the file by XMLHTTP method. i can get the file but the file format is not the same with the file i sent. thanks for any suggestions ..

    <script language="javascript">
    var url = "ajax/ajaxFile.aspx";
    var binary;
    var filename;
    var mytext;
    var _folder="";

    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="pathFile"' + '\n'
        + '\n'
        + _folder + '\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>

    ------------------------------------------

    and my asp.net code as

     if (Request["pathFile"] != null)
            {
                string strPath = Request["pathFile"].ToString().Trim();
                Response.Write(strPath+"<br/>");
                if (Request.Files.Count > 0)
                {
                    try
                    {
                        for (int i = 0; i < Request.Files.Count; i++)
                        {

                            int values = Request.Files[i].FileName.LastIndexOf('\\');
                            string filename = Request.Files[i].FileName.Substring(values + 1);
                            String strSavePath = Server.MapPath(ConfigurationManager.AppSettings["NewsImage"]) + "\\" + strPath + "\\" + filename;
                            Response.Write(strSavePath);
                            Request.Files[i].SaveAs(strSavePath)
                            //if (!CheckImageSize(strSavePath, 100, 100))
                            //{
                            //    WebLibs.Globals.ResizeImage(strSavePath, 100, 100);
                            //}
                        }
                    }
                    catch (Exception objEx)
                    {
                        Response.Write(objEx.Message);
                    }
                }

     --------------------------------------------------------------

     

  • Re: javascript sending file problem when recive

    07-02-2008, 11:05 PM
    Answer
    Hi hoaiduc2304
     
    It seems your code from here: http://www.captain.at/ajax-file-upload.php
     
    And just as it said, first, you must configuration your browser settings.
     
    And in that simple, they write server-side by php, I am not familiar with php, but I notice that:
     
    // javascript "escape" does not encode the plus sign "+", but "urldecode"
    //        in PHP make a space " ". So replace any "+" in the file with %2B first
    $filename = $fpath.$_FILES{myfile}{name};
    $handle = fopen($filename, "r");
    $contents = fread($handle, filesize($filename));
    fclose($handle);
     
    $contents = preg_replace("/\+/", "%2B", $contents);
     
    It seems may be you need some encode convert action in your server-side code.
     
    Actually, if you are using ASP.NET and AJAX, I suggest you to upload file with Web Service or WCF.
    And the following links may helps:
     
    AJAX file upload
    http://www.codeproject.com/KB/aspnet/AJAXUpload.aspx
     
    How to send cross site request in Ajax?
    http://www.easywms.com/easywms/?q=zh-hans/how-send-cross-site-request-ajax
     
    Simple AJAX File Upload
    http://www.codeproject.com/KB/ajax/simpleajaxupload.aspx
     
    MultiUpload, a smarter file upload server control using AJAX
    http://www.codeproject.com/KB/ajax/MultiUpload.aspx
     
    If I’ve misunderstood your problem, please feel free to let me know.
     
    Thanks.
     

     

    Lance Zhang
  • Re: javascript sending file problem when recive

    07-05-2008, 9:59 AM
    • Member
      1 point Member
    • hoaiduc2304
    • Member since 05-24-2007, 4:34 AM
    • Posts 6

    dear Lance,

    Your ref is really great but i just wanna know how to convert the file when i receive from client . do u have any ideas thanks for ur responding .  

  • Re: javascript sending file problem when recive

    07-06-2008, 7:53 AM
    • All-Star
      76,865 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,327
    • TrustedFriends-MVPs

    AFAIK, there is no "magic" way. You would need to read the file and convert it yourself.

    NC...

  • Re: javascript sending file problem when recive

    07-06-2008, 11:44 AM
    • Member
      1 point Member
    • hoaiduc2304
    • Member since 05-24-2007, 4:34 AM
    • Posts 6

    i know there is no magic way, but i try alot of my code but Problem is always troublesome. Just wanna ask , wat is the format file when i recive from sever , i guess it is binary but i use streambinary to convert and nothing happen T_T ...any ideas please.

Page 1 of 1 (5 items)