danilo carrillo

danilo carrillo

  • NA
  • 11
  • 594

does not save the server side file

Jul 31 2019 9:16 PM
In this code I am using the asmx service using ajax, my files are stored in localhost, but when I upload it to a server on the internet it does not save, I gave it full control permissions in IIS. and I still do not find the problem.
  1. var form_data = new FormData();  
  2.   
  3.  form_data.append("Archivo", $('#txtArchivo')[0].files[0])  
  4.  form_data.append("Fecha", $("#txtTitulo").val())  
  5.  form_data.append("Descripcion", $("#txtActuaciones").val())  
  6.  form_data.append("Id", $("#txtRadicadoOculto").val())  
  7.  form_data.append("Carpeta", $("#inputSuccess").val())  
  8.  console.log("datos" + JSON.stringify(form_data))  
  9.  var param = {  
  10.               Id: $("#txtRadicadoOculto").val(),  
  11.               Fecha: $("#txtTitulo").val(),  
  12.               Descripcion: $("#txtActuaciones").val(),  
  13.               Archivo: form_data  
  14.               };  
  15. console.log("con el archivo" + JSON.stringify(param));  
  16.   
  17. //  formData.append("file", file);  
  18. $.ajax({  
  19.         url: '<%=ResolveUrl("ArchivoActuacion.asmx/Guardar")%>',  
  20.         type: 'POST',  
  21.         data: form_data,  
  22.         // cache: false,  
  23.         processData: false// Don't process the files  
  24.         contentType: false// Set content type to false as jQuery will tell the server its a query string request  
  25.         beforeSend: function () {  
  26.             $("#load").fadeIn(100);  
  27.             $('#loading').css({  
  28.                 'display''block'  
  29.             });  
  30.         },  
  31.         success: function (data) {  
  32.             console.log("mi respuesta" + data.success)  
  33.             $("#load").fadeOut(200);  
  34.             if (data.success != true) {  
  35.                 alert("No pudo guardarse! " + data.success)  
  36.             } else {  
  37.                 alert("Guardado Satisfactoriamente!")  
  38.                 $("#txtTitulo").val("")  
  39.                 $("#txtActuaciones").val("")  
  40.                 $('#txtArchivo').val("");  
  41.             }  
  42.   
  43.             //alert("Guardado satisfactoriamente")  
  44.         },  
  45.         error: function (XMLHttpRequest, textStatus, errorThrown) {  
  46.                    var err = eval("(" + XMLHttpRequest.responseText + ")");  
  47.                    alert("ojo" + err.Message)  
  48.                    console.log("Ajax Error!");  
  49.                    $("#load").fadeOut(200);  
  50.                }  
  51.            });  
  1. public class ArchivoActuacion : System.Web.Services.WebService {
  2. public ArchivoActuacion()  
  3. {  
  4.   
  5.     //Elimine la marca de comentario de la línea siguiente si utiliza los componentes diseñados   
  6.     //InitializeComponent();   
  7. }  
  8.   
  9. [WebMethod]  
  10. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]  
  11. public void Guardar()  
  12. {  
  13.     HttpContext Contexto = HttpContext.Current;  
  14.   
  15.     string radicado = Contexto.Request.Form["Id"];  
  16.     // Byte[] archi = Contexto.Request.Form["archivo"]  
  17.     string fecha = Contexto.Request.Form["Fecha"];  
  18.     string descripcion = Contexto.Request.Form["Descripcion"];  
  19.     string carpeta = Contexto.Request.Form["Carpeta"];  
  20.     HttpFileCollection ColeccionArchivos = Context.Request.Files;  
  21.   
  22.   
  23.     string NombreArchivo = "";  
  24.     string directorio = "";  
  25.     int ArchivoActual = 0;  
  26.   
  27.     SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConLegasis"].ConnectionString);  
  28.   
  29.   
  30.     if (ColeccionArchivos.Count != 0)  
  31.     {  
  32.   
  33.         Contexto.Server.ScriptTimeout = 2400;  
  34.         NombreArchivo = ColeccionArchivos[0].FileName;  
  35.         String DatosArchivo = System.IO.Path.GetFileName(ColeccionArchivos[0].FileName);  
  36.         string filename = string.Format("{0}{1:yyyyMMdd_HHmmss}.{2}", Path.GetFileNameWithoutExtension(ColeccionArchivos[ArchivoActual].FileName), DateTime.Now, Path.GetExtension(ColeccionArchivos[ArchivoActual].FileName));  
  37.         string cadena = filename.Replace(" ", String.Empty);  
  38.         // hpf.SaveAs(Server.MapPath(txtId.Value + "\\") + Path.GetFileName(cadena));  
  39.         if (Directory.Exists(Server.MapPath(carpeta)))  
  40.         {  
  41.             // String CarpetaParaGuardar = Server.MapPath("files") + "\\" + cadena;  
  42.             String CarpetaParaGuardar = Server.MapPath(carpeta + "\\") + Path.GetFileName(cadena);  
  43.             ColeccionArchivos[0].SaveAs(CarpetaParaGuardar);  
  44.             directorio = "./" + carpeta + "/" + Path.GetFileName(cadena);// para sql  
  45.             var strSQL = "INSERT INTO Actuacion_simple VALUES(" + radicado + ",'" + fecha + "', '" + descripcion.ToUpper().ToString() + "','" + directorio + "')";  
  46.             var cmd = new SqlCommand(strSQL, cn);  
  47.             cn.Open();  
  48.             cmd.ExecuteNonQuery();  
  49.             cn.Close();  
  50.             Contexto.Response.ContentType = "application/json";  
  51.             Contexto.Response.Write("{\"success\":true,\"msg\":\"" + NombreArchivo + "\"}");  
  52.             Contexto.Response.End();  
  53.   
  54.         }  
  55.      else  
  56.     {  
  57.             Directory.CreateDirectory(Server.MapPath(carpeta));  
  58.             String CarpetaParaGuardar = Server.MapPath(carpeta + "\\") + Path.GetFileName(cadena);  
  59.             ColeccionArchivos[0].SaveAs(CarpetaParaGuardar);  
  60.             directorio = "./" + carpeta + "/" + Path.GetFileName(cadena);// para sql  
  61.             var strSQL = "INSERT INTO Actuacion_simple VALUES(" + radicado + ",'" + fecha + "', '" + descripcion.ToUpper().ToString() + "','" + directorio + "')";  
  62.             var cmd = new SqlCommand(strSQL, cn);  
  63.             cn.Open();  
  64.             cmd.ExecuteNonQuery();  
  65.             cn.Close();  
  66.             Contexto.Response.ContentType = "application/json";  
  67.             Contexto.Response.Write("{\"success\":true,\"msg\":\"" + NombreArchivo + "\"}");  
  68.             Contexto.Response.End();  
  69.         }  
  70. }  
  71.     else  
  72.     {  
  73.         directorio = "";  
  74.         var strSQL = "INSERT INTO Actuacion_simple VALUES(" + radicado + ",'" + fecha + "', '" + descripcion.ToUpper().ToString() + "','" + directorio + "')";  
  75.         var cmd = new SqlCommand(strSQL, cn);  
  76.         cn.Open();  
  77.         cmd.ExecuteNonQuery();  
  78.         cn.Close();  
  79.         Contexto.Response.ContentType = "application/json";  
  80.         Contexto.Response.Write("{\"success\":true,\"msg\":\"" + NombreArchivo + "\"}");  
  81.         Contexto.Response.End();  
  82.     }  
  83.   
  84.   
  85.   
  86.   
  87. }  
  88. }

Answers (2)