简体   繁体   中英

Not able to save file from dynamically generated fileupload controls

I have a project wherein fileupload controls are generating dynamically. Using Json, I'm trying to save file in a directory asynchronously. I want to save files with filename using handler template in asp.net 4.0.

        $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: Sitepath + "Handler/FileUploader.ashx?filename="+strfiles,
        secureuri: false,
        fileElementClass: "multi",
        dataType: "json",
        async: false
    });

I've added HTTPPostedFile's logic outside FileUploader class as it was throwing error in that.

public class FileUploader : IHttpHandler, IRequiresSessionState

{

public void ProcessRequest(HttpContext context)
{
    string rt = "";


    HttpContext.Current.Response.ContentType = "text/HTML";
    string urlresponse = HttpContext.Current.Request.Url.ToString();
    string tempfiles = "";
    string filenames = "";
    int convert = urlresponse.IndexOf(",");
    urlresponse = urlresponse.Substring(convert + 1);
    string[] filesArray = urlresponse.Split(',');
    for (int i = 0; i < filesArray.Length; i++)
    {
        tempfiles = filesArray[i].ToString();
        int lstIndex=tempfiles.LastIndexOf("\\");
        filenames = filenames + "," + tempfiles.Substring(lstIndex + 1);
    }
    filenames = filenames.Substring(filenames.IndexOf(',') + 1);





    HttpFileCollection uploads = HttpContext.Current.Request.Files;
   string b = HttpContext.Current.Request.Url.ToString();

    Hashtable hashtable = new Hashtable();

    // Declare variable
    string OrderFileName = String.Empty;
    string OrderIDs =String.Empty;
    string TempFolder = String.Empty;

    if (HttpContext.Current.Session["OrderID"] != null)
    {

        OrderIDs = HttpContext.Current.Session["OrderID"].ToString();

        string mapPath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["DocPath"].ToString());

        string mapPathUserId = mapPath + "\\" + HttpContext.Current.Session["UserID"];
      ///
        var g = filenames.Split(',');

        for (int t = 0; t < g.Length;t++ )
        {

           var h = g[t];
           rt = filesArray[t].ToString();

            if (Directory.Exists(mapPathUserId) == false)
            {
                Directory.CreateDirectory(mapPathUserId);
            }
            string mapPathCount = mapPathUserId + "/" + OrderIDs;
            if (Directory.Exists(mapPathCount) == false)
            {
                //--------------Begin(Create Directory)----------------------------------------------------------------------------------//

                Directory.CreateDirectory(mapPathCount);

                //--------------End(Create Directory)----------------------------------------------------------------------------------//

            }
            TempFolder = mapPathUserId + "/" + "Temp";

            if (Directory.Exists(TempFolder) == false)
            {
                //--------------Begin(Create Directory for  temp folder)----------------------------------------------------------------------------------//

                Directory.CreateDirectory(TempFolder);

                //--------------End(Create Directoryfor temp folder)----------------------------------------------------------------------------------//

            }

            OrderFileName = h;
            hashtable.Add(t.ToString(), OrderFileName);

            var see = HttpContext.Current.Server.MapPath(TempFolder + "/" + OrderFileName);
        }

Now, path is being created successfully, but file is not getting saved in specified directory. My code to save the file:

   for (int i = 0; i < uploads.Count; i++)
        {
            HttpPostedFile upload = uploads[i];

            if (Directory.Exists(mapPathUserId) == false)
            {
                Directory.CreateDirectory(mapPathUserId);
            }

            string mapPathCount = mapPathUserId + "/" + OrderIDs;


            if (Directory.Exists(mapPathCount) == false)
            {
                //--------------Begin(Create Directory)----------------------------------------------------------------------------------//

                Directory.CreateDirectory(mapPathCount);

                //--------------End(Create Directory)----------------------------------------------------------------------------------//

            }

            /// Create Path for Temp Folder


            TempFolder = mapPathUserId + "/" + "Temp";

            if (Directory.Exists(TempFolder) == false)
            {
                //--------------Begin(Create Directory for  temp folder)----------------------------------------------------------------------------------//

                Directory.CreateDirectory(TempFolder);

                //--------------End(Create Directoryfor temp folder)----------------------------------------------------------------------------------//

            }

            if (upload.ContentLength > 0)
            {

                if (upload.FileName.Contains(" "))
                {
                    OrderFileName = upload.FileName.Replace(" ", "_");
                }

                else
                {
                    OrderFileName = upload.FileName;

                }

                hashtable.Add(i.ToString(), OrderFileName);
                upload.SaveAs(TempFolder + "/" + OrderFileName);
            }

        }

Please help.

You have to make sure you pass a right path, in the server . I would try mapping the path with the MapPath() function:

upload.SaveAs(Server.MapPath(TempFolder + "/" + OrderFileName));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM