表单响应文件上传

时间:2016-09-05 12:45:07

标签: c# sitefinity

在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);

2 个答案:

答案 0 :(得分:0)

您无法设置上传控件的值,因为这将是一个巨大的安全漏洞。只有用户才能选择要上传的文件。

答案 1 :(得分:0)

要以编程方式将文件附加到表单响应,您需要使用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();
    }