表单响应文件上传

本文关键字:文件 响应 表单 | 更新日期: 2023-09-27 18:05:37

在sitefinity中,我的表单包含一个文件上传控件。如何将文件上传值设置为已选择的文件?我试图以编程方式设置表单的值。下面是我到目前为止写的:

 //Enquiry Test
                entry.SetValue("FormParagraphTextBox_C007", "Test sample");
                int count = 0;
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    var file = Request.Files[i];
                    var contentTtype = Request.Files[i].ContentType;
                    string extension = Path.GetExtension(file.FileName).ToLower();
                    if (ValidFileFormats.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries).Contains(extension))
                    {
                        MIMETypeChecker mc = new MIMETypeChecker();
                        if (contentTtype == mc.GetMIMEType(file.FileName))
                        {
                            byte[] fileBytes;
                            using (var binaryReader = new BinaryReader(file.InputStream))
                            {
                                fileBytes = binaryReader.ReadBytes(Request.Files[i].ContentLength);
                            }
                            DirectoryInfo dir = new DirectoryInfo(ConfigurationManager.AppSettings["EnquiriesUploadPath"] + Member.MemberNumber + "/" + refNrNe.ReferenceNumber);
                            if (!dir.Exists)
                                dir.Create();
                            if (dir.GetFiles().Count() < 11)
                            {
                                using (FileStream fs = new FileStream(dir.FullName + "/" + file.FileName, FileMode.Create, FileAccess.ReadWrite))
                                {
                                    fs.Write(fileBytes, 0, fileBytes.Length);                                       
                                }
                            }
                        }
                    }
                }
                //Attatchment Upload
                entry.SetValue("FormFileUpload_C008", null);

表单响应文件上传

您不能设置上载控件的值,因为这将是一个巨大的安全漏洞。只有用户可以选择上传哪个文件

对于以编程方式将文件附加到表单响应,您需要使用ContentLink的一个简短的例子:

    void CreateFormResponse()
    {
        FormsManager formsMgr = FormsManager.GetManager();
        formsMgr.Provider.SuppressSecurityChecks = true;
        var form = formsMgr.GetFormByName("sf_stackoverflow");
        if (form != null)
        {
            var entryType = String.Format("{0}.{1}", formsMgr.Provider.FormsNamespace, form.Name);
            FormEntry entry = formsMgr.CreateFormEntry(entryType);
            //here you can populate text fields
            entry.SetValue("FormTextBox_C001", "fieldvalue");

            var documentContentLinks = UploadDocument(entry);
            entry.SetValue("FormFileUpload_C003", documentContentLinks);
            entry.IpAddress = Request.UserHostAddress;
            entry.SubmittedOn = DateTime.UtcNow;
            entry.UserId = ClaimsManager.GetCurrentUserId();
            if (SystemManager.CurrentContext.AppSettings.Multilingual)
            {
                entry.Language = CultureInfo.CurrentUICulture.Name;
            }
            entry.ReferralCode = formsMgr.Provider.GetNextReferralCode(entryType).ToString();
            formsMgr.SaveChanges();
        }
    }
    ContentLink[] UploadDocument(FormEntry entry)
    {
        LibrariesManager libraryManager = LibrariesManager.GetManager();
        var contentLinksManager = ContentLinksManager.GetManager();
        //here you need to choose proper document, or upload your document
        var document = libraryManager.GetDocuments().FirstOrDefault();
        ContentLink contentLink = new ContentLink();
        contentLink.Id = Guid.NewGuid();
        contentLink.ParentItemId = entry.Id;
        contentLink.ParentItemType = entry.GetType().ToString();
        contentLink.ChildItemId = document.Id;
        contentLink.ComponentPropertyName = entry.Id.ToString();
        contentLink.ChildItemAdditionalInfo = "additional info";
        contentLink.ChildItemProviderName = (((IDataItem)document).Provider as DataProviderBase).Name;
        contentLink.ChildItemType = document.GetType().FullName;
        contentLink.ApplicationName = contentLinksManager.Provider.ApplicationName;
        ContentLink[] contentLinks = new ContentLink[0];
        if (contentLinks == null)
        {
            contentLinks = new ContentLink[0];
        }
        var assetsFieldList = contentLinks.ToList();
        assetsFieldList.Insert(0, contentLink);
        return assetsFieldList.ToArray();
    }