Developer Forum »
Uploading file and setting file description
58 posts

I'm uploading a file via the Request.Files collection along with other data in the Request.Form collection. I start by deleting any previous file and adjusted versions before setting the file:

if (files != null && files["imageFile"] != null)
{
       HttpPostedFile file = (HttpPostedFile)files["imageFile"];

       var contentLength = file.ContentLength;
       if (contentLength > 0)
       {
             var fileName = file.FileName;
             var inputStream = file.InputStream;
             byte[] fileData = null;
             using (var binaryReader = new System.IO.BinaryReader(inputStream))
             {
                    course.Image.DeleteCachedAdjustments();
                    course.Image.DeleteFile();
                    fileData = binaryReader.ReadBytes(contentLength);
                    course.Image.SetFile(fileData, fileName);
                    course.UpdateChanges();
             }
       }
}

I then go ahead and make the other changes:

if (form["imageName"] != null) course.Image.Description = form["imageName"];

After this, I make a final call to UpdateChanges().

When debugging and watching the properties, it all seems good, but when viewing it in WAF afterwards, the description is not persisted.

Why is this? If I only change the description and not the file, everything works fine.

6 posts

I think Webnodes uses the MetaData.Description on imagefiles so you could try to change:

course.Image.Description = form["imageName"];

to

course.Image.MetaData.Description = form["imageName"];
58 posts

I have already tried this. Works the same way.. It works just fine when not setting the file. As soon as the file is being set, it seems like the metadata (I'm not sure about other fields) is not being persisted.

120 posts

Have you tried this:

if (files != null && files["imageFile"] != null){
       HttpPostedFile file = (HttpPostedFile)files["imageFile"];
       if (file.ContentLength > 0){
             var fileName = file.FileName;
             var inputStream = file.InputStream;
             byte[] fileData = null;
             using (var binaryReader = new System.IO.BinaryReader(inputStream)){
                    fileData = binaryReader.ReadBytes(contentLength);
                    course.Image.SetFile(fileData, fileName);
                    course.Image.Description = form["imageName"] + "";
                    course.UpdateChanges();
             }
       }
}
58 posts

Hmmm.. :) I'm pretty sure I tried that first, but now it actually seems to work just fine, doing it in the order you suggested. Thanks!

1