View Single Post
  #3 (permalink)  
Old February 28th, 2012, 02:02 PM
sophia sophia is offline
Friend of Wrox
Points: 1,469, Level: 15
Points: 1,469, Level: 15 Points: 1,469, Level: 15 Points: 1,469, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

HI I am trying to upload images using httphandler.

for this I have few settings in web.config as below.

<httpHandlers>
<remove verb="POST,GET" path="Upload.axd" />
<add verb="POST,GET" path="Upload.axd" type="Upload" />
</httpHandlers>

in aspx page I am having a control to upload files, which in turn transfers the control to the Upload.cs class in App_code folder, where code is as follows (I am logged in at the time of uploading files)



public class Upload : IHttpHandler, IRequiresSessionState
{
public Upload()
{

}

#region IHttpHandler Members

public bool IsReusable
{
get { return true; }
}

public void ProcessRequest(HttpContext context)
{
using (ProjectEntities myEntities = new ProjectEntities())
{
MyFile myFile;
string virtualFolder = "~/Images/";
//string uploadPath = context.Server.MapPath(context.Request.Application Path + virtualFolder);
string uploadPath = context.Server.MapPath(virtualFolder);
// loop through all the uploaded files
for(int j = 0; j < context.Request.Files.Count; j++)
{
// get the current file
HttpPostedFile uploadFile = context.Request.Files[j];
// if there was a file uploded
if (uploadFile.ContentLength > 0)
{

string fileName = Guid.NewGuid().ToString();

string extension = Path.GetExtension(uploadFile.FileName);

myFile = new MyFile();
myFile.Name = uploadFile.FileName;
myFile.Url = virtualFolder + fileName + extension;
myFile.Owner = (Guid)Membership.GetUser().ProviderUserKey;

//myFile.Owner = (Guid)Membership.GetUser(HttpContext.Current.User. Identity.Name).ProviderUserKey;
myEntities.AddToMyFile(myFile);
myEntities.SaveChanges();

uploadFile.SaveAs(Path.Combine(uploadPath, fileName + extension));}

}


}
}

}



The code breaks at when storing logged in user's id. Please help me.

Thanks
Reply With Quote