Click here to Skip to main content
15,904,348 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I got always null value for this,

What I have tried:

Controller,

C#
if (itm.AttachmentModel.AttachmentFile != null)
               {
                   itm.AttachmentModel.AttachmentURL = string.Format("{0}.{1}", Guid.NewGuid().ToString(), itm.AttachmentModel.AttachmentFile.FileName.Split('.')[1]);
                   if (itm.AttachmentModel.AttachmentFile != null)
                   {

                       var path = Path.Combine(Server.MapPath("~/App_Data/Attachments/"), itm.AttachmentModel.AttachmentURL);
                       itm.AttachmentModel.AttachmentFile.SaveAs(path);
                   }
               }


View,

HTML
<input type="hidden" value="" name="..."><input type="file" name="AttachmentFile">


Modal,

C#
public HttpPostedFileBase AttachmentFile { get; set; }
    public string AttachmentURL
    {
        get
        {
            return attachment;
        }

        set
        {
            attachment = value;
        }
    }

    public string Attachment
    {
        get
        {
            return attachment;
        }

        set
        {
            attachment = value;
        }
    }
Posted
Updated 28-Nov-16 17:54pm
Comments
Suvendu Shekhar Giri 28-Nov-16 0:08am    
"I got always null value for this"
for what?
Ehsan Sajjad 28-Nov-16 11:25am    
the above information is not enough to figure out what you did wrong

1 solution

Hello, I am sharing one example for get the image(file) on post method:



[HttpPost]
public ActionResult GetImage(HttpPostedFileBase file)
{
//you can put your existing save code here
if (file != null && file.ContentLength > 0)
{
//do whatever you want with the file
}
}

Hope it's helpful for you.
 
Share this answer
 
Comments
T_Sub 30-Nov-16 8:19am    
Thank You !

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