2016-09-05 66 views
0

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

回答

0

您無法設置上傳控件的值,因爲這將是一個巨大的安全漏洞。只有用戶可以選擇要上傳哪個文件。

+0

用戶是選擇files.Hence的「Request.Files」我現在開始認爲這可能是我最好的選擇。 [鏈接](http://www.sitefinity.com/developer-network/forums/developing-with-sitefinity-/how-to-programmatically-associate-document-upload-to-form-response-entry) –

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(); 
    }