k2docattachment upload file

  • 24 July 2008
  • 1 reply
  • 2 views

Badge

Is there a way to change the name of an uploaded file when using a k2docattachment control?
if an user upload a file named "aa.txt", i would like to save as "aa123.txt".
I already tried to save to a temporary folder, but i'm not figuring out how to associate to k2docattachment control
the "saved as" file instead the posted file.
Thanks.


1 reply

Badge +1

as it was made in our project using server event in K2.Net Studio:


Public Sub Main(ByRef K2 As ServerEventContext)


 ' Set up variables
 Dim ErrorMessage As String


 Dim DocsField As String
 DocsField = K2.ProcessInstance.XmlFields("AttachedDocuments1").Value


 Dim DestServer As String
 DestServer = "http://" & K2.StringTable("k2_server")
  
 If Not DestServer.EndsWith("/") Then DestServer &= "/"


 Dim Param0 As System.String = Trim(K2.ProcessInstance.DataFields("CompanyName").Value.ToString.Replace("&", "and"))
 Dim Param1 As String
 
 Param1="Contracts%20Under%20Approval/"+Param0
 
 Dim DestFolder As String
 DestFolder = Param1


 Dim SpsList As New K2SPSList()
 Dim SpsUtils As New SourceCode.K2SPUtilities.SPSUtilities


 Dim oXmlDoc As New System.Xml.XmlDocument
 oXmlDoc.LoadXml(DocsField)
 Dim oXmlNode As System.Xml.XmlNode
 Dim oXmlNodeList As System.Xml.XmlNodeList


 oXmlNodeList = oXmlDoc.SelectNodes("spsdocuments/documents/document")


 ' Check if the XML fields are initialized
 InitializeXmlField(K2.ProcessInstance.XmlFields("AttachedDocuments1"))


 Dim j As Int32 = 0
 Dim SSS,SS,EXTN As String
 Dim MyParam As Integer = 0
 
 For Each oXmlNode In oXmlNodeList
   MyParam=MyParam+1
   'If MyParam<2 Then
       j += 1
   EXTN=Microsoft.VisualBasic.Right(oXmlNode.SelectSingleNode("docname").InnerText, 4)   
   If j>1 Then
       'if there are more than 1 document we have to add an index to filenames
       EXTN = "-"+CType((j-1), String)+EXTN
   End If
  
   If K2.ProcessInstance.DataFields("DocType").Value="some_value" Or K2.ProcessInstance.DataFields("DocType").Value="some_another_value" Then
 SSS = K2.ProcessInstance.DataFields("DocType").Value & " " & K2.ProcessInstance.DataFields("ContractNum").Value & EXTN
 Else
 SSS = K2.ProcessInstance.DataFields("DocType").Value & " " & K2.ProcessInstance.DataFields("ContractNum").Value & " to Contract " & K2.ProcessInstance.DataFields("AppendixNum").Value & EXTN
  End If
  SS=replace(SSS,"/","-")
  
 
  Dim oByte() As Byte
  Dim Server As String
  Server = oXmlNode.SelectSingleNode("server").InnerText
  If Not Server.EndsWith("/") Then Server &= "/"


  ' Set Url for Web Service
  SpsList.Url = Server & "_vti_bin/K2SpsList.asmx"
  ' Set Credentials
  SpsList.Credentials = SpsUtils.GetCredentials(Server)


  ' Call Web Service to Retrive the Document
  If Not SpsList.GetDocument(Server, oXmlNode.SelectSingleNode("site").InnerText, oXmlNode.SelectSingleNode("folder").InnerText, _
      oXmlNode.SelectSingleNode("docname").InnerText, oByte, ErrorMessage) Then
   ' Error Occurred in GetDocument - Raise Error
   Throw New System.Exception(ErrorMessage)
  End If


  ' Call Web Service to Upload Document
  SpsList.Credentials = SpsUtils.GetCredentials(DestServer)


  SpsList.Url = DestServer & "_vti_bin/K2SpsList.asmx"
  If Not SpsList.UploadDocument(DestServer, K2.StringTable("k2_site"), DestFolder, _
      SS, oByte, False , ErrorMessage) Then
  ' Error Occurred in UploadDocument - Raise Error
  Throw New System.Exception(ErrorMessage)
  End If


  ' Call Web Service to Delete Document
  If Not SpsList.DeleteDocument(oXmlNode.SelectSingleNode("site").InnerText, oXmlNode.SelectSingleNode("folder").InnerText, _
     oXmlNode.SelectSingleNode("docname").InnerText, ErrorMessage)
   ' Error Occurred in DeleteDocument - Raise Error
   Throw New System.Exception(ErrorMessage)
  End If


  ' Update the Attahment field
  Dim sAtt As String
  sAtt = K2.ProcessInstance.XmlFields("AttachedDocuments1").Value
  sAtt = SpsUtils.UpdateAttachmentField(sAtt, j, _
   DestServer, K2.StringTable("k2_site"), DestFolder, SS)
  K2.ProcessInstance.XmlFields("AttachedDocuments1").Value = sAtt


  'End If
 Next


End Sub


Private Sub InitializeXmlField(ByRef oXmlField As SourceCode.KO.XmlField)
 If oXmlField.Value Is Nothing OrElse oXmlField.Value = "" Then
  ' Check if Meta Data exist to initialize the field
  If oXmlField.MetaData Is Nothing OrElse oXmlField.MetaData = "" Then
   Throw New System.Exception("The " & oXmlField.Name & " XML field could not be initialized.")
  Else
   oXmlField.Value = oXmlField.MetaData
  End If
 End If
End Sub

Reply