Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for WebAssembly
  4. Can QML FileDialog Be Used to Select Upload File from Web Browser?
Forum Updated to NodeBB v4.3 + New Features

Can QML FileDialog Be Used to Select Upload File from Web Browser?

Scheduled Pinned Locked Moved Unsolved Qt for WebAssembly
3 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • stickabeeS Offline
    stickabeeS Offline
    stickabee
    wrote on last edited by stickabee
    #1

    I am trying to create a pop-up dialog in my browser-based Qt/QML application in order to select a file on my workstation to upload to the unit running the web server. I am unsure what "context" the FileDialog is operating in when invoked via button click in the browser. Is it local to the unit that's running the server and hosting the page or is it local to the workstation running the browser?

    I've got the following code in QML, taken from an example in the FileDialog documentation:

    FileDialog {
        id: fileDialog
        title: "Please choose a file"
    
        folder: shortcuts.home
        onAccepted: {
            console.log("You chose: " + fileDialog.fileUrls)
            //Do the data_client stuff and get the file moving
            client.getUploadFile(fileUrls)
        }
        onRejected: {
            console.log("Canceled")
        }
    }
    

    This dialog is displayed with the following:

    Button {
    	id: uploadButton
    	x: 224
    	y: 14
    	text: qsTr("Upload")
    	onClicked: {
    		fileDialog.open()
    	}
    }                            
    
    

    The problem I'm seeing currently is that, when opened, the dialog gives me some filesystem "space" that I have no idea about. Regardless of whether I'm running on a Windows or Linux machine, I get the following dialog, in the "/home/web_user" directory:

    Dialog Filesystem

    To my layman's brain, this would seem to indicate that the dialog is local to the unit hosting the server(linux-based, where my workstation is USUALLY Windows-based), but I can't find that directory structure anywhere on that unit. Is it some special context that exists in the web server (lighttpd)? Is this some userspace in the context of the browser? Is there something I need to do to "point" the FileDialog at the local filesystem on the workstation I want to upload the file from?

    I'm not well-versed in web development lingo/tools, hence my attempt to use Qt/WebAssembly to create a web-based GUI, so please forgive my using terms that may not be technically correct...

    Edit: I now generally understand that the filesystem I'm seeing is a product of emscripten, though I don't see a way of getting access to the filesystem of the machine running the browser. I am wrapping my head around the browser "sandbox" concept and trying to understand how that interacts with the filesystem emscripten is creating. Is what I'm trying to do possible in Qt? HTML5 can accomplish it, and I see examples of javascript being able to do it, so I think I'm just lacking some basic understanding of what Qt needs from me...

    stickabeeS 1 Reply Last reply
    0
    • stickabeeS stickabee

      I am trying to create a pop-up dialog in my browser-based Qt/QML application in order to select a file on my workstation to upload to the unit running the web server. I am unsure what "context" the FileDialog is operating in when invoked via button click in the browser. Is it local to the unit that's running the server and hosting the page or is it local to the workstation running the browser?

      I've got the following code in QML, taken from an example in the FileDialog documentation:

      FileDialog {
          id: fileDialog
          title: "Please choose a file"
      
          folder: shortcuts.home
          onAccepted: {
              console.log("You chose: " + fileDialog.fileUrls)
              //Do the data_client stuff and get the file moving
              client.getUploadFile(fileUrls)
          }
          onRejected: {
              console.log("Canceled")
          }
      }
      

      This dialog is displayed with the following:

      Button {
      	id: uploadButton
      	x: 224
      	y: 14
      	text: qsTr("Upload")
      	onClicked: {
      		fileDialog.open()
      	}
      }                            
      
      

      The problem I'm seeing currently is that, when opened, the dialog gives me some filesystem "space" that I have no idea about. Regardless of whether I'm running on a Windows or Linux machine, I get the following dialog, in the "/home/web_user" directory:

      Dialog Filesystem

      To my layman's brain, this would seem to indicate that the dialog is local to the unit hosting the server(linux-based, where my workstation is USUALLY Windows-based), but I can't find that directory structure anywhere on that unit. Is it some special context that exists in the web server (lighttpd)? Is this some userspace in the context of the browser? Is there something I need to do to "point" the FileDialog at the local filesystem on the workstation I want to upload the file from?

      I'm not well-versed in web development lingo/tools, hence my attempt to use Qt/WebAssembly to create a web-based GUI, so please forgive my using terms that may not be technically correct...

      Edit: I now generally understand that the filesystem I'm seeing is a product of emscripten, though I don't see a way of getting access to the filesystem of the machine running the browser. I am wrapping my head around the browser "sandbox" concept and trying to understand how that interacts with the filesystem emscripten is creating. Is what I'm trying to do possible in Qt? HTML5 can accomplish it, and I see examples of javascript being able to do it, so I think I'm just lacking some basic understanding of what Qt needs from me...

      stickabeeS Offline
      stickabeeS Offline
      stickabee
      wrote on last edited by
      #2

      I'm incredibly angry with myself for not having read the documentation closely enough.

      I had to change from the QGuiApplication to a QApplication in order to use widgets, add widgets to my project "QT +=" line, change the QML to call the backend instead of trying to invoke the FileDialog directly there in the QML, and call the appropriate function, as specified in the QFileDialog documentation...

      The QML is now:

      Button {
      	id: uploadButton
      	x: 224
      	y: 14
      	text: qsTr("Upload")
      	onClicked: {
      		client.getUploadFile();
      	}
      }
      

      In the backend:

      auto fileContentReady = [](const QString &fileName, const QByteArray &fileContent) {
          if (fileName.isEmpty()) {
              // No file was selected
          } else {
              qDebug() << "File name: " + fileName;
          }
      };
      
      void Data_Client::getUploadFile()
      {
          QFileDialog::getOpenFileContent("Images (*.png *.xpm *.jpg)",  fileContentReady);
      

      It's all pretty clearly defined in the documentation for getOpenFileContent(), but I had to understand so much just to realize what I wasn't seeing... Hope this helps people having a similar problem...

      P 1 Reply Last reply
      3
      • stickabeeS stickabee

        I'm incredibly angry with myself for not having read the documentation closely enough.

        I had to change from the QGuiApplication to a QApplication in order to use widgets, add widgets to my project "QT +=" line, change the QML to call the backend instead of trying to invoke the FileDialog directly there in the QML, and call the appropriate function, as specified in the QFileDialog documentation...

        The QML is now:

        Button {
        	id: uploadButton
        	x: 224
        	y: 14
        	text: qsTr("Upload")
        	onClicked: {
        		client.getUploadFile();
        	}
        }
        

        In the backend:

        auto fileContentReady = [](const QString &fileName, const QByteArray &fileContent) {
            if (fileName.isEmpty()) {
                // No file was selected
            } else {
                qDebug() << "File name: " + fileName;
            }
        };
        
        void Data_Client::getUploadFile()
        {
            QFileDialog::getOpenFileContent("Images (*.png *.xpm *.jpg)",  fileContentReady);
        

        It's all pretty clearly defined in the documentation for getOpenFileContent(), but I had to understand so much just to realize what I wasn't seeing... Hope this helps people having a similar problem...

        P Offline
        P Offline
        pvillacorta
        wrote on last edited by
        #3

        @stickabee Could you provide a more detailed implementation?
        Thank you :)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved