Página 1 de 1

Subida de archivos a servidor error

Publicado: 03 Ago 2018 19:18
por Guanj
Este es mi codigo directamente me paraliza la aplicacion y tengo que esperar un rato luego veo que no ha subido nada

no puedo usar la dll de pabloko porque lo que quiero es recibir el estado de la subida y poner una barra de la subida cambiando lua socket por el lua de ams tampoco puedo usar ftp debido a la seguridad nuclear de ams
path = _SourceFolder.."\\AutoPlay\\Images\\gris.png"

size = File.GetSize(path)


http = require("socket.http")
ltn12 = require("ltn12")

http.request{
    url = "mihost.com/upload.php",
    method = "POST",
    headers = {
        ["Content-Type"] =  "multipart/form-data",
        ["Content-Length"] = size
    },
    source = ltn12.source.file(io.open(path)),
    sink = ltn12.sink.table(response_body)
}

Dialog.Message("Notice", response_body[1], MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);


<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/files/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo "Temp filename = " . $_FILES['userfile']['tmp_name'] ;

echo '\nHere is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>

Re: Subida de archivos a servidor error

Publicado: 04 Ago 2018 09:22
por Pabloko
multipart-post.lua

local ltn12 = require "ltn12"

local fmt = function(p, ...)
if select('#', ...) == 0 then
return p
else return string.format(p, ...) end
end

local tprintf = function(t, p, ...)
t[#t+1] = fmt(p, ...)
end

local append_data = function(r, k, data, extra)
tprintf(r, "content-disposition: form-data; name=\"%s\"", k)
if extra.filename then
tprintf(r, "; filename=\"%s\"", extra.filename)
end
if extra.content_type then
tprintf(r, "\r\ncontent-type: %s", extra.content_type)
end
if extra.content_transfer_encoding then
tprintf(
r, "\r\ncontent-transfer-encoding: %s",
extra.content_transfer_encoding
)
end
tprintf(r, "\r\n\r\n")
tprintf(r, data)
tprintf(r, "\r\n")
end

local gen_boundary = function()
local t = {"BOUNDARY-"}
for i=2,17 do t = string.char(math.random(65, 90)) end
t[18] = "-BOUNDARY"
return table.concat(t)
end

local encode = function(t, boundary)
boundary = boundary or gen_boundary()
local r = {}
local _t
for k,v in pairs(t) do
tprintf(r, "--%s\r\n", boundary)
_t = type(v)
if _t == "string" then
append_data(r, k, v, {})
elseif _t == "table" then
assert(v.data, "invalid input")
local extra = {
filename = v.filename or v.name,
content_type = v.content_type or v.mimetype
or "application/octet-stream",
content_transfer_encoding = v.content_transfer_encoding or "binary",
}
append_data(r, k, v.data, extra)
else error(string.format("unexpected type %s", _t)) end
end
tprintf(r, "--%s--\r\n", boundary)
return table.concat(r), boundary
end

local gen_request = function(t)
local boundary = gen_boundary()
local s = encode(t, boundary)
return {
method = "POST",
source = ltn12.source.string(s),
headers = {
["content-length"] = #s,
["content-type"] = fmt("multipart/form-data; boundary=%s", boundary),
},
}
end

return {
encode = encode,
gen_request = gen_request,
}


ejemplo:

local mp = (require "multipart-post").gen_request
local H = (require "socket.http").request
local rq = mp{myfile = {name = "myfilename", data = "some data"}}
rq.url = "http://httpbin.org/post"
local b,c,h = H(rq)

Re: Subida de archivos a servidor error

Publicado: 04 Ago 2018 16:18
por Guanj
Donde pone

local rq = mp{myfile = {name = "myfilename", data = "some data"}}

que tengo que poner en some data?

He probado esto y no funciona me da error
local filename = _SourceFolder.."\\AutoPlay\\Images\\gris.png"
local file = io.open(filename, "rb")
local contents = file:read( "*a" )






local mp = (require "multipart-post").gen_request

local H = (require "socket.http").request


local rq = mp{myfile = {name = "gris.png", data = contents}}

rq.url = "mihost.com/upload.php"

local b,c,h = H(rq)

Re: Subida de archivos a servidor error

Publicado: 04 Ago 2018 22:00
por Pabloko
y cual es el error?xd

para que case con ese php deberia ser

Código: Seleccionar todo

local rq = mp{userfile= {name = "xxx.png", data = "..."}}

Re: Subida de archivos a servidor error

Publicado: 05 Ago 2018 00:06
por Guanj
gracias pabloko esto ya sube cosas cuando arregle amshub bien te pondre en los credits :penguin:



solo eran 4 cambios y una mezcla de todo xD
local file = io.open(_SourceFolder.."\\AutoPlay\\Images\\blurred_colors.png", "rb")
local contents = file:read( "*a" )






local mp = (require "multipart-post").gen_request

local H = (require "socket.http").request


rq = mp{userfile= {name = _SourceFolder.."\\AutoPlay\\Images\\blurred_colors.png", data = contents}}


rq.url = "mihost.com/upload.php"

local b,c,h = H(rq)

local gen_boundary = function()

  t = {"BOUNDARY-"}

  for i=2,17 do t = string.char(math.random(65, 90)) end

  t[18] = "-BOUNDARY"

  return table.concat(t)

end



local function gen_boundary()
	local t = {"BOUNDARY-"}
	for i = 2, 17 do
		t[i] = string.char(math.random(65, 90))
	end
	t[18] = "-BOUNDARY"
	return table.concat(t)
end

<?php

// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead

// of $_FILES.



$uploaddir = 'files/';

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);



echo '<pre>';

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {

echo "File is valid, and was successfully uploaded.\n";

} else {

echo "Possible file upload attack!\n";

}

echo "Temp filename = " . $_FILES['userfile']['tmp_name'] ;



echo '\nHere is some more debugging info:';

print_r($_FILES);



print "</pre>";



?>


Re: Subida de archivos a servidor error

Publicado: 05 Ago 2018 02:11
por Guanj
estoy mirando y aunque sube el archivo no me envia el resultado de la subida osea la respuesta del php

Re: Subida de archivos a servidor error

Publicado: 05 Ago 2018 14:54
por Guanj
en teoria es un http.request tendria que devolver lo que pone en el php