Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
upload.ashx

ASP.NET
<%@ WebHandler Language="C#" Class="Upload" %>


C#
using System;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Configuration;
using System.Web.SessionState;

public class Upload : IHttpHandler, IReadOnlySessionState
{     
         public void ProcessRequest (HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Expires = -1;       
      
        try
        {
            
         HttpPostedFile postedFile = context.Request.Files["Filedata"];
            
            string savepath = "";

            string tempPath = ConfigurationManager.AppSettings["Filedata"];
        
            savepath = context.Server.MapPath(tempPath);
            string filename = postedFile.FileName;
            if (!Directory.Exists(savepath))
                Directory.CreateDirectory(savepath);

            postedFile.SaveAs(savepath + @"\" + filename);
           
            context.Response.Write(tempPath + "/" + filename);
            context.Response.StatusCode = 200;
            if (postedFile != null)
            {
                context.Session["img"] = postedFile.FileName;
              
            }
            else
            {
                context.Session["img"] ="No File Uploaded";
              
            }
        }
        catch (Exception ex)
        {
            context.Session["img"] = "Throwed exception when uploading file";
            
            
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
 
}

Document.aspx.cs

if (Session["img"].ToString() != null)
{
   // this session return correct value in chrome 
  //but firefox it return null value only
}

Advance Thanks
Posted
Updated 27-Nov-15 20:01pm
v2
Comments
Suvendu Shekhar Giri 28-Nov-15 0:14am    
Put a break point in the upload function and debug till end of the function and check if the upload is happening correctly. Then share your findings here.
SaravanaKumar@sk 28-Nov-15 0:25am    
Yes, i check it upload is correctly saved, but i need that file name, that why am using session
[no name] 28-Nov-15 0:41am    
Go to web.config file and find the word "Filedata". Then you will find path where it storing. As Suvendu told, did you put break point where the file is storing...
SaravanaKumar@sk 28-Nov-15 1:03am    
sir, files are uploaded, i just want to store file name in session, http handler page it also stored in session and i want transfer that session value to aspx.cs page

web.config
<add key="FolderPath" value="uploads">
[no name] 28-Nov-15 1:49am    
Session["filName"]=postedFile.FileName;

For a suggestion your requirement doesn't match with question header. Please try to give proper content to get better response. Cheers

Instead of converting the Session value to string check directly for NULL.
Try this-
C#
if (Session["img"] != null)
{
   //put your logic here
}


Hope, it helps :)
 
Share this answer
 
Comments
SaravanaKumar@sk 28-Nov-15 5:18am    
This my sample code:

if(Session["img"] != null)
{
Hide Copy Code
txtUploadFile.SaveAs(SharedPath + "\" + Session["img"].ToString());
}
Suvendu Shekhar Giri 28-Nov-15 5:25am    
Removing .ToString() from
if(Session["img"].ToString() != null)
didn't helped ?
This my sample code:

if(Session["img"] != null)
{
C#
txtUploadFile.SaveAs(SharedPath + "\\" + Session["img"].ToString());

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900