I need to upload a file in chrome, and some parameters need posted on the same request I want to use JavaScript AJAX to do this, and basic certification is required. But chrome does not support sendAsBinary, how can I do this?
function sendMsg (status) {var user = localStorage.getObject (CURRENT_USER_KEY); Var file = $ ("# imagefile") [0]. File [0]; Var range = '---- multipartformboundary' + (new date) .getTime (); Var dashdash = '-'; Var CRLF = '\ r \ n'; / * Create RFC 2388 string * / var builder = ''; Builder + = Dashdash; Builder + = Border; Builder + = CRLF; Var xhr = New XMLHttpRequest (); Var Upload = xhr.upload; Xhr.onreadystatechange = function () {if (xhr.readyState == 4) {//}}; If (upload) {upload.onprogress = function (ev) {onprogress (ev)); }; } / * Generate Header [STATUS] * / Builder + = 'Content-Vision: Form-Data; Name = "status" '; Builder + = CRLF; Builder + = CRLF; / * Add Form Data * / Builder + = msg; Builder + = CRLF; / Write range * / builder + = dashdash; Builder + = Border; Builder + = CRLF; / * Generate Header [PIC] * / Builder + = 'Content-Vision: Form-Data; Name = "picture" '; If (file.fileName) {builder + = '; Filename = "'+ file.fileName +' '' ';} Builder + = CRLF; Builder + =' Content-Type: '+ file.type; Builder + = CRLF; Builder + = CRLF; / * Add Binary Data * / Builder + = file.getAsBinary (); // chrome getAsBinary () builder + do not support CRLF; / write limit * / builder + = dashdash; builder + = limit; builder + = cfd; / * request Mark End * / Builder + = DashDash; Builder + = Limit; Builder + = DashDash; Builder + = CRLF; Xhr.open ("Post", apiUrl.sina.upload, true); Xhr.setRequestHeader Xhr.ssetRequestHeader ('authorization', make_base_auth_header (user.userName, user.password)); xhr.sendAsBinary (builder); // Chrome Remittance Do not support asburyi (xhr.onload = function) {/ * if we have an error displayed. * / If (xhr.responseText) {console.log (xhr.responseText);}};}; < / Code>
Use xhr.send (FormData) . You do not have to build multipart-form-data myself :) See my response here:
Comments
Post a Comment