Jump to content

Quick Question about Downloads


Controlled

Recommended Posts

You can use fileRead to read the content of the .txd file.

Once that is done, you can use triggerClientEvent and pass the content so that you use fileCreate and then you save the file with txd and you finally use engineImportTXD

I am having trouble following.

fileRead("mod.txd")  
triggerClientEvent(source, "runmod",  source)  

For server? Then what were you saying about client

Link to comment
You should not use triggerClientEvent since it almost blocks network activity.

If you want to transfer a lot of data, you should use triggerLatentClientEvent instead.

Example:

local content = fileRead(file, fileGetSize(file)) -- if you wanna open huge files, use a smaller buffer instead to save memory 
triggerLatentClientEvent(source, "runmod", root, content) 

Thanks! Is there any way to have a progress bar of some sorts? so it tells you what your progress is?

and I'm not sure what you mean about opening huge files?

Link to comment

This is how I had it written..

Server:

    function download() 
    local open = fileOpen("BUyJfGFCMAEe5BI.jpg") 
    local content = fileRead("BUyJfGFCMAEe5BI.jpg", fileGetSize("BUyJfGFCMAEe5BI.jpg")) -- if you wanna open huge files, use a smaller buffer instead to save memory 
    triggerLatentClientEvent(source, "runMod", resourceRoot, content) 
    end 
addCommandHandler("dicks", download) 
  

Client

  
addEvent("runMod", true) 
function runMod() 
 local hfile = fileOpen("BUyJfGFCMAEe5BI.jpg", false) 
   if hfile then 
   outputDebugString("You have it") 
   else 
   fileCreate("BUyJfGFCMAEe5BI.jpg") 
   fileWrite(hfile, content) 
   outputDebugString("Done") 
   end 
   end 
addEventHandler("runMod", resourceRoot, runMod) 
  

I am getting errors from Server Side saying bad file pointer. Also Expected String at Argument 1 got nil. For triggerLatentEvent

Thats simply to download it, not to apply anything

Link to comment
This is how I had it written..

[...]

I am getting errors from Server Side saying bad file pointer. Also Expected String at Argument 1 got nil. For triggerLatentEvent

Thats simply to download it, not to apply anything

There you have your code, fixed:

Server:

function _downloadFile ( player ) 
    local file = fileOpen ( "BUyJfGFCMAEe5BI.jpg", true ) 
    if ( file ) then 
        local content = fileRead ( file, fileGetSize ( file ) ) 
        triggerLatentClientEvent ( player, "runMod", resourceRoot, content ) 
        -- You can now use 'getLatentEventHandles' and 'getLatentEventStatus' 
        -- to get the status of the latent event 
        -- You may as well mess with triggerLatentClientEvent's bandwidth parameter 
        -- if the download speed is too slow 
    else 
        outputChatBox ( "Couldn't find file to download", player ) 
    end 
end 
addCommandHandler ( "dicks", _downloadFile ) 

Your mistake here was the use of the file name in file functions where they expected a file element, which was returned in the fileOpen call.

Client:

function runMod ( content ) 
    local file = fileOpen ( "BUyJfGFCMAE5BI.jpg" ) 
    if ( file ) then 
        outputDebugString ( "You already have the file" ) 
        fileClose ( file ) 
    else 
        file = fileCreate ( "BUyJfGFCMAE5BI.jpg" ) 
        fileWrite ( file, content ) 
        fileClose ( file ) 
        outputDebugString ( "Done" ) 
    end 
end 
addEvent ( "runMod", true ) 
addEventHandler ( "runMod", root, runMod ) 

Here, you forgot to close the file handles and you messed up everything in the part where you write the content to the file. I would suggest you to check whether the files the client has are not modified, by the way.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...