LPology / Simple-Ajax-Uploader

Javascript file upload plugin with progress bar support. Works in all major browsers, including IE7+, Chrome, Firefox, Safari, and Opera. No dependencies - use it with or without jQuery.
995 stars 267 forks source link

Error to upload file to server #214

Open danielcuautle opened 5 years ago

danielcuautle commented 5 years ago

I'm try to upload file to server, but the browser console write this:

SimpleAjaxUploader.js:1828 POST ..../SaveFiles.asmx/Save 500 (Internal Server Error) _uploadXhr @ SimpleAjaxUploader.js:1828 _initUpload @ SimpleAjaxUploader.js:1882 submit @ SimpleAjaxUploader.js:1160 (anonymous) @ SimpleAjaxUploader.js:2046

markusramsak commented 5 years ago

Is your server url correct? If yes, then your server side code might produce some error. Try to simplify your code, so you can locate the source of the error for specific.

LPology commented 5 years ago

Can you post the server side code for handling the upload and the Javascript code you're using?

danielcuautle commented 5 years ago

CLIENT:

SERVER:

using RedRingQuiscoDB.Modelos.Seguridad; using RedRingQuiscoDB.Utileria; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Hosting; using System.Web.Script.Services; using System.Web.Services;

namespace RedRingQuioscoWeb.Views.Home { ///

/// Summary description for SaveFiles /// [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class SaveFiles : System.Web.Services.WebService {

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void Save()
    {
        try
        {
            HttpContext Contexto = HttpContext.Current;
            HttpFileCollection vFilesCollection = Context.Request.Files;

            Usuario usuario = (Usuario)(HttpContext.Current.Session["Login_Usuario"]);
            string vPath = HostingEnvironment.MapPath($"~/Recursos/temp/{usuario.IdUsuario}");

            if (Directory.Exists(vPath))
            {
                string[] filePaths = Directory.GetFiles(vPath);
                foreach (string filePath in filePaths)
                    File.Delete(filePath);
            }

            string vNameFile = string.Empty;
            for (int x = 0; x < vFilesCollection.Count; x++)
            {
                vNameFile = vFilesCollection[x].FileName;
                string vFileInfo = System.IO.Path.GetFileName(vFilesCollection[x].FileName);
                string vFolderToSave = $"{Server.MapPath("Recursos")}\\{vFileInfo}";

                if (!Directory.Exists(vPath))
                {
                    Directory.CreateDirectory(vPath);
                }

                string[] words = vNameFile.Split('.');
                string vTypeFile = words[words.Length - 1];
                vFilesCollection[x].SaveAs($"{vPath}/{vFileInfo}");
                Contexto.Response.ContentType = "application/json";
                Contexto.Response.Write("{\"success\":true,\"msg\":\"" + vNameFile + "\",\"fileName\":\"" + vNameFile + "\",\"folderName\":\"" + usuario.IdUsuario + "\",\"typeFile\":\"" + vTypeFile + "\"}");
                Contexto.Response.End();
            }
        }
        catch (Exception ex)
        {
            Log.Instances.RegistroError(ex);
        }
    }
}

}