rest - How to upload a file from a sheet (via VBA macro) to a laravel application? -
i'm trying make spreedsheet "upload itself" restful web service made in laravel 4.
i have web form same work, need to, instead of make user go web application , manually upload file, make sheet capable of upload click of button (using macros). have method receives input::file('filename') , open file read , stuff. i'm using microsoft.xmlhttp vba object send request ws. saddly, ain't seem able upload god damm file! i'm sending path (absolute path) in post method, isn't working.
the problem is: how within vba code? how upload file server through vba code? and, if possible, how made compatible laravel application?
edit
to proper answer @andreyco's question, i'm making edit.
that's receive in vba debug tool when return dump of input::all()
array ( [spreedsheet] => c:\users\android\desktop\tarifa.xls ) ...but, when receive response web form, looks this.
array ( [_token] => rvtklep6rwvkvvxc3u0woo6nyldylp9xi36n6gb2 [spreedsheet] => symfony\component\httpfoundation\file\uploadedfile object ( [test:symfony\component\httpfoundation\file\uploadedfile:private] => [originalname:symfony\component\httpfoundation\file\uploadedfile:private] => tarifa.xls [mimetype:symfony\component\httpfoundation\file\uploadedfile:private] => application/vnd.ms-excel [size:symfony\component\httpfoundation\file\uploadedfile:private] => 43520 [error:symfony\component\httpfoundation\file\uploadedfile:private] => 0 [pathname:splfileinfo:private] => /tmp/phprsx5bf [filename:splfileinfo:private] => phprsx5bf ) ) ... because of laravel structure , stuff. hope useful.
here complete, working example. if not need "please wait" dialog use first code snippet , delete uploadthisfilemain thereof. note server php test script @ end.
sub uploadthisfilemain() if activeworkbook.saved = false msgbox "this workbook contains unsaved changes. please save first." exit sub end if dim ret ret = startprocessing("file uploading, please wait...", "uploadthisfile") if (ret = true) msgbox "upload successful!" else msgbox "upload failed: " & ret end if end sub private function uploadthisfile() dim bound string bound = "a0ad2346-9849-4ef0-9a93-acfe17910734" dim url string url = "https://<yourserver>/index.php?id={" & bound & "}" dim path string path = thisworkbook.path & "\" & thisworkbook.name smultipart = pvgetfileasmultipart(path, bound) on error resume next dim r r = pvpostmultipart(url, smultipart, bound) if err.number <> 0 uploadthisfile = err.description err.clear else uploadthisfile = true end if end function 'sends multipart/form-data url using winhttprequest/xmlhttp 'formdata - binary (vt_ui1 | vt_array) multipart form data private function pvpostmultipart(url, formdata, boundary) dim http 'as new msxml2.xmlhttp 'create xmlhttp/serverxmlhttp/winhttprequest object 'you can use of these 3 objects. 'set http = createobject("winhttp.winhttprequest.5") 'set http = createobject("msxml2.xmlhttp") set http = createobject("msxml2.serverxmlhttp") 'open url post request http.open "post", url, false 'set content-type header http.setrequestheader "content-type", "multipart/form-data; boundary=" + boundary 'send form data url post binary request http.send formdata 'get result of script has received upload pvpostmultipart = http.responsetext end function private function pvgetfileasmultipart(sfilename string, boundary string) byte() dim nfile integer dim spostdata string '--- read file nfile = freefile open sfilename binary access read nfile if lof(nfile) > 0 redim babuffer(0 lof(nfile) - 1) byte nfile, , babuffer spostdata = strconv(babuffer, vbunicode) end if close nfile '--- prepare body spostdata = "--" & boundary & vbcrlf & _ "content-disposition: form-data; name=""uploadfile""; filename=""" & mid$(sfilename, instrrev(sfilename, "\") + 1) & """" & vbcrlf & _ "content-type: application/octet-stream" & vbcrlf & vbcrlf & _ spostdata & vbcrlf & _ "--" & boundary & "--" '--- post pvgetfileasmultipart = pvtobytearray(spostdata) end function private function pvtobytearray(stext string) byte() pvtobytearray = strconv(stext, vbfromunicode) end function create new module processing_code:
public processing_message string public macro_to_process string public return_value string function startprocessing(msg string, code string) processing_message = msg 'set message displayed 'in dialog box macro_to_process = code 'set macro run after 'dialog box active processing_dialog.show 'show dialog box startprocessing = return_value end function create form processing_dialog. set startupposition 2 - centerscreen. add code:
private sub userform_initialize() lblmessage.caption = processing_message 'change label 'caption end sub private sub userform_activate() me.repaint 'refresh userform return_value = application.run(macro_to_process) 'run macro unload me 'unload userform end sub now add button worksheet (if there no "developer" tab, go "options" -> "customize ribbon" -> enable checkbox "developer") , assign macro uploadthisfilemain.
for server part use php test script:
<?php foreach (getallheaders() $name => $value) { echo "$name: $value\n"; } echo "post:"; print_r($_post); echo "get:"; print_r($_get); echo "files:"; print_r($_files); $entitybody = file_get_contents('php://input'); echo "body:$entitybody"; exit; $base_dir = dirname( __file__ ) . '/upload/'; if(!is_dir($base_dir)) mkdir($base_dir, 0777); move_uploaded_file($_files["uploadfile"]["tmp_name"], $base_dir . '/' . $_files["uploadfile"]["name"]); ?> sources:
Comments
Post a Comment