Buenas, segun veo un interes creciente en esto y tenia por ahi algunos trozos de codigo de otra cosa asi que los meto en una dll.
Se trata de subir archivos a un servidor mediante formulario, sin contraseñas ni historias, simplemente hay que poner un archivo php (o cualquier otro tipo) en el server para recibir los archivos y utilizar la dll para enviarlos.
bool uploaded = UploadFile(string URL, string FILE);
Parte AMS
Parte PHP
Codigo Fuente
Descarga: -- LINK CAÍDO --
MODERADORES: SI TIENES EL ARCHIVO, POR FAVOR CONTACTA A UN MODERADOR (ADMINISTRADOR, MIEMBRO DE STAFF O CO-STAFF) PARA MODIFICAR EL LINK DE DESCARGA
Se trata de subir archivos a un servidor mediante formulario, sin contraseñas ni historias, simplemente hay que poner un archivo php (o cualquier otro tipo) en el server para recibir los archivos y utilizar la dll para enviarlos.
bool uploaded = UploadFile(string URL, string FILE);
Parte AMS
result = DLL.CallFunction("AutoPlay\\Docs\\WebUploadPabloko.dll", "UploadFile", "\"http://localhost:8080/up.php\",\"".._SourceFolder.."\\AutoPlay\\Docs\\imagen.png\"", DLL_RETURN_TYPE_INTEGER, DLL_CALL_STDCALL);
<?
$status = "";
if ($_POST["action"] == "upload") {
// obtenemos los datos del archivo
$tamano = $_FILES["file"]['size'];
$tipo = $_FILES["file"]['type'];
$archivo = $_FILES["file"]['name'];
$prefijo = substr(md5(uniqid(rand())),0,6);
if ($archivo != "") {
// guardamos el archivo a la carpeta files
$destino = "files/".$prefijo."_".$archivo;
if (copy($_FILES['file']['tmp_name'],$destino)) {
$status = "Archivo subido: <b>".$archivo."</b>";
} else {
$status = "Error al subir el archivo";
}
} else {
$status = "Error al subir archivo";
}
}
?>
Codigo Fuente
using System;
using System.Collections.Generic;
using System.Text;
using RGiesecke.DllExport;
using System.Collections.Specialized;
using System.Net;
using System.IO;
namespace WebUploadPabloko
{
internal static class UnmanagedExports
{
[DllExport("UploadFile", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static bool uploadfile(string url, string file)
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("action", "upload");
return HttpUploadFile(url, file, "file", MimeType(file), nvc);
}
public static bool HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
{
//log.Debug(string.Format("Uploading {0} to {1}", file, url));
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
bool ret;
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;
wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream rs = wr.GetRequestStream();
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach (string key in nvc.Keys)
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, key, nvc[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
rs.Write(formitembytes, 0, formitembytes.Length);
}
rs.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string header = string.Format(headerTemplate, paramName, file, contentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();
WebResponse wresp = null;
try
{
wresp = wr.GetResponse();
Stream stream2 = wresp.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
//log.Debug(string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd()));
ret = true;
}
catch (Exception ex)
{
//log.Error("Error uploading file", ex);
ret = false;
if (wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
wr = null;
}
return ret;
}
public static string MimeType(string Filename)
{
string mime = "application/octetstream";
string ext = System.IO.Path.GetExtension(Filename).ToLower();
Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (rk != null && rk.GetValue("Content Type") != null)
mime = rk.GetValue("Content Type").ToString();
return mime;
}
}
}
Descarga: -- LINK CAÍDO --
MODERADORES: SI TIENES EL ARCHIVO, POR FAVOR CONTACTA A UN MODERADOR (ADMINISTRADOR, MIEMBRO DE STAFF O CO-STAFF) PARA MODIFICAR EL LINK DE DESCARGA