The 1C:Enterprise developers forum

#1
People who like this: 0 Yes / 0 No
Active user
Rating: 5
Joined: Jun 4, 2013
Company:

Hi, I'm wondering if I can attache a PDF file into a documnet or catalog record.

And thanks for being helpful in advance

 
#2
People who like this: 0 Yes / 0 No
Timofey Bugaevsky
Guest

Joined:
Company:

Yes, you need to create an attribute of ValueStorage type and load your file into it.

 
#3
People who like this: 0 Yes / 0 No
Active user
Rating: 5
Joined: Jun 4, 2013
Company:

Hi, I did what you've told me ... i think it's working but i can't find a way to open it, I'll show you my code.

here the user can choose the pdf from a browser  

Code
&AtClient
Procedure Command1(Command)
                   
    Dialog = New FileDialog(FileDialogMode.Open);
    If Dialog.Choose() Then
        pdf= ss(Dialog.FullFileName);
    EndIf;

EndProcedure

&AtServer
function ss(dialog)

    pdf= new ValueStorage (dialog,New Deflation(9));
    Return pdf      

EndFunction

Do you have any suggestion or recommendation?

 
#4
People who like this: 0 Yes / 0 No
Timofey Bugaevsky
Guest

Joined:
Company:

You were trying to put a file name into the value storage. Please see How to store a file in the database? topic for example.

 
#5
People who like this: 0 Yes / 0 No
Active user
Rating: 5
Joined: Jun 4, 2013
Company:

Ok i think i did it right ... look at my code now:

Code
&AtClient
Procedure LoadFile()
   StorageName = "";
   
    FileName ="C:\Users\mohammad.maleh\Desktop\1C.pdf";
    PutFile(StorageName, FileName, FileName, True, UUID);
    LoadFileServer(StorageName);
      
EndProcedure


&AtServer
Procedure LoadFileServer(StorageName)

   ObjectValue = FormDataToValue(Object, Type("DocumentObject.Document1"));
   ObjectValue.storage = New ValueStorage(GetFromTempStorage(StorageName));
   ObjectValue.Write();
   ValueToFormData(ObjectValue, Object);
  
   EndProcedure



now how can i open the PDF ?!!! ...sorry for the huge amount of questions today .. but this thing is really difficult :D

 
#6
People who like this: 0 Yes / 0 No
Timofey Bugaevsky
Guest

Joined:
Company:

No problems.

To do this you need to get your file:

Code
&AtServer
Procedure OnCreateAtServer(Cancel, StandardProcessing)
   ObjectValue = FormDataToValue(Object, Type("DocumentObject.Document1"));
   StorageName = PutToTempStorage(ObjectValue.storage.Get(), UUID);
EndProcedure

&AtClient
Procedure ExportFile(Command)
   If Not IsBlankString(StorageName) Then
      GetFile(StorageName, Object.Description, True);
   EndIf;
EndProcedure

Here StorageName is a form attribute with String type.

 
#7
People who like this: 0 Yes / 0 No
Active user
Rating: 5
Joined: Jun 4, 2013
Company:

I added this to the previous code as you said ...as a result a new window is opened and asked me to <open> <save> <cancel> the file ... when i press open nothing is happening


Code

&AtServer
    Procedure OnCreateAtServer(Cancel, StandardProcessing)
        ObjectValue = FormDataToValue(Object, Type("DocumentObject.Document1"));
   StorageName = PutToTempStorage(ObjectValue.storage.Get(), UUID);
    EndProcedure


&AtClient
Procedure pdfview(Command)
   Message(StorageName);
         If Not IsBlankString(StorageName) Then
      GetFile(StorageName,"1C Ebook", True);
   EndIf;

    EndProcedure



sorry again brother for bothring you :)

 
#8
People who like this: 0 Yes / 0 No
Timofey Bugaevsky
Guest

Joined:
Company:

I guess that you are uploading file and right after that trying to see it. As storage name is updated in OnCreateAtServer handler, the file that you have uploaded will not be accessible until you will open your document form again. So you need to update storage name after uploading file:

Code
&AtServer
Procedure LoadFileServer(StorageName)
­
    ObjectValue = FormDataToValue(Object, Type("DocumentObject.Document1"));
    ObjectValue.storage = New ValueStorage(GetFromTempStorage(StorageName));
    ObjectValue.Write();
    ValueToFormData(ObjectValue, Object);

    UpdateStorageName();
  
EndProcedure

&AtServer
Procedure OnCreateAtServer(Cancel, StandardProcessing)
    
    UpdateStorageName();

EndProcedure

&AtServer
Procedure UpdateStorageName()

    ObjectValue = FormDataToValue(Object, Type("DocumentObject.Document1"));
    StorageName = PutToTempStorage(ObjectValue.storage.Get(), UUID);

EndProcedure


And please pay attention to idents in your code as they make code more readable.

 
#9
People who like this: 0 Yes / 0 No
Active user
Rating: 5
Joined: Jun 4, 2013
Company:

This Code worked with me perfectly ... and i just wanna share it whit you guys

Code
&AtClient
Procedure Upload(Command)
    LoadFile();
EndProcedure
         &AtClient
Procedure LoadFile()   
   Object.StorageName = "";
    FileName ="C:\Users\mohammad.maleh\Desktop\hello1c.pdf"; 
   Object.Description=FileName;
   PutFile(Object.StorageName,Object.Description, Object.Description, True, UUID);

   LoadFileServer(Object.StorageName);
            
     EndProcedure


&AtServer
Procedure LoadFileServer(StorageName)
   ObjectValue = FormDataToValue(Object, Type("DocumentObject.Document1"));
   ObjectValue.storage = New ValueStorage(GetFromTempStorage(StorageName));
    ObjectValue.Write();
   ValueToFormData(ObjectValue, Object);

   updateStorageName()

EndProcedure

&AtServer
Procedure OnCreateAtServer(Cancel, StandardProcessing)
   updateStorageName()
EndProcedure


&AtClient
Procedure pdfview(Command)
   
   If Not IsBlankString(Object.StorageName) Then
         GetFile(Object.StorageName,Object.Description, true);
    EndIf;
      

EndProcedure

&AtServer
Procedure UpdateStorageName()
         
   ObjectValue = FormDataToValue(Object, Type("DocumentObject.Document1"));
   Object.StorageName = PutToTempStorage(ObjectValue.storage.Get(), UUID);
   
EndProcedure



where storage is a object attribute with valuestorage type and description and storagename are object attributes with string type.

Edited: mohammad maleh - Nov 11, 2013 05:26 AM
 
#10
People who like this: 0 Yes / 0 No
Timofey Bugaevsky
Guest

Joined:
Company:

Also please note that if you put file to temp storage when form is created, you make form opening slower, but on the other side your PDF file will be opened faster when a user will execute command. You could also move UpdateStorageName() to PDFView() instead of OnCreateAtServer():

Code
&AtClient
Procedure PDFView(Command)
   
    If Not IsBlankString(Object.StorageName) Then
        UpdateStorageName()
        GetFile(Object.StorageName,Object.Description, true);
    EndIf;
­
EndProcedure

The variant depends on what is more importand. I suppose that in your case this variant would be more efficient.

 
#11
People who like this: 0 Yes / 0 No
Interested
Rating: 11
Joined: Nov 10, 2011
Company: 1A Software e.U

Dear Mohammad,

I had a very similar task, and a technical specialist of 1C company helped me. I don't know if he likes I mention his name. But I believe he doesn't mind if I share this information with you.

At first we load a file

Code
&AtClient
Procedure LoadFile1(Command)
   StorageName1 = "";
   FileName = Object.FileName;
   If PutFile(StorageName1, FileName, FileName, True, UUID) Then
    LoadFileServer1(StorageName1);
   EndIf;
EndProcedure


Here is "If PutFile(...True) then"  important, because a user can just close this form and don't attach a file.

Then we save it into the Storage. In my case it is an attribute Storage1 (I have 4 of them)

Code
&AtServer
Procedure LoadFileServer1(StorageName1)
   ObjectValue = FormDataToValue(Object, Type("DocumentObject.Project"));
   ObjectValue.Storage1 = New ValueStorage(GetFromTempStorage(StorageName1));
   ObjectValue.Write();
   ValueToFormData(ObjectValue, Object);



And then

Code
&AtClient
Procedure OpenFile1(Command)
   
   TempFileName = GetTempFileName("pdf"); // I need only pdf-files
   StorageURL = GetURL(Object.Ref, "Storage1");
   GetFile(StorageURL, TempFileName, False);   
   RunApp(TempFileName);

EndProcedure


StorageURL is important, because we can not directly access Object.Storage1.    GetFile(...) saves the file onto the disc and RunApp(...) is a function to open this file.

And I do not use

Code
//ObjectValue = FormDataToValue(Object, Type("DocumentObject.Project"));
    //StorageName = PutToTempStorage(ObjectValue.Storage.Get(), UUID);


I hope it helps. Good luck!

 
#12
People who like this: 0 Yes / 0 No
Active user
Rating: 5
Joined: Jun 4, 2013
Company:

Thanks for Sharing this Lioudmila, it's very helpful for me and for the other people :)

 
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)