开发者

form with multiple upload but allow no upload on edit problems

开发者 https://www.devze.com 2022-12-24 03:36 出处:网络
i have a section that when created takes in images, however when you edit this item i dont want them to re-upload none changes images just to change a description or name.

i have a section that when created takes in images, however when you edit this item i dont want them to re-upload none changes images just to change a description or name.

i have created this that deals with uploading files:

public void UploadFiles(string currentFileName, FormCollection form)
    {   // loop through all files in form post
        foreach (string file in Request.Files)
        {
            HttpPostedFileBase hpf = Request.Files[file];
            // if no file is uploaded, we could be editing so set to current value
            if (hpf.ContentLength == 0)
            {
                form[file] = currentFileName;
            }
            else
            {   //rename the file unique so we dont clash with names
                var filename = hpf.FileName.Replace(" ", "_").Replace(".", DateTime.Now.Date.Ticks + ".");

                UploadFileName = filename;
                hpf.SaveAs(Server.MapPath("~/Content/custom/" + filename));
                // set the name of the file in our post to the new name
   开发者_StackOverflow中文版             form[file] = UploadFileName;
            }

        }

        // ensure value is still sent when no files are uploaded on edit
        if(Request.Files.Count <= 0)
        {
            UploadFileName = currentFileName;
        }
    }

all works fine when only one image is required (CurrentFileName), however there is now a new image available taking it to a total of 2 images in the database therefor currentFileName is obsolete. has anyone tackled this and how as i have hit a wall with this one. thought of string[] currentFiles but cant see how to match this into string file in Request.Files.

if it helps i am also working with models for the form so i could pass over the model but i dont think your able to do model.file without some kind of reflection.

help much appreciated.

thanks


solution eventually straightened my head out

public void UploadFiles(FormCollection form, NameValueCollection currentFiles)
    {
        foreach (string file in Request.Files)
        {
            HttpPostedFileBase hpf = Request.Files[file];

            if (hpf.ContentLength == 0)
            {
                form[file] = currentFiles[file];
            }
            else
            {
                var filename = hpf.FileName.Replace(" ", "_").Replace(".", DateTime.Now.Date.Ticks + ".");

                UploadFileName = filename;
                hpf.SaveAs(Server.MapPath("~/Content/custom/" + filename));

                form[file] = UploadFileName;
            }

        }

        if(Request.Files.Count <= 0)
        {
            foreach (var file in currentFiles.AllKeys)
            {
                form[file] = currentFiles[file];
            }
        }
    }

anyfeedback and sudgestions would be appreciated

0

精彩评论

暂无评论...
验证码 换一张
取 消