Angular.js中的文件上传问题

时间:2018-06-22 06:53:18

标签: angularjs

我正在使用以下代码上传 .csv文件,它可以完美运行。但是我在上传过程中遇到了一个问题。当我第一次选择.csv时,提交按钮将被启用,但是当我选择错误的文件而不是.csv时,按钮不会被禁用。如果我要清除所选文件,则清除按钮不起作用。我不是想出什么问题。

下面是发生上传文件功能的控制器代码:

HTML:

@QtCore.pyqtSlot(...)

Angularjs:

import sys
import datetime
import time

from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtCore import *
from PyQt4.QtGui import *

# Link to GUI
qtCreatorFile = "GUInterface.ui"

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

def time_converter_to_unix(start_datetime, end_datetime):
    # Convert QTimeEdit to UNIX Timestamp (int, msec included), and then to float
    start_datetime_unix_int = start_datetime.toMSecsSinceEpoch ()
    start_datetime_unix = (float(start_datetime_unix_int) / 1000)

    end_datetime_unix_int = end_datetime.toMSecsSinceEpoch ()
    end_datetime_unix = (float(end_datetime_unix_int) / 1000)

    return start_datetime_unix, end_datetime_unix

def dummy_function(self, start_datetime_unix, end_datetime_unix): 
    # Dummy function, just to simulate a task. It takes parameters just for testing.
    result = start_datetime_unix * end_datetime_unix 

    # Pre-steps for mapping from one range to other (progress-bar)
    OldMax = 99999
    OldMin = 1
    NewMax = 100
    NewMin = 1

    OldRange = (OldMax - OldMin)
    NewRange = (NewMax - NewMin)    

    u = 1
    for i in range (OldMax):
        u = i*2

        OldValue = i
        print(OldValue)
        NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin
        print("NEW VALUE: ", NewValue)

        self.cpuValueChanged.emit(NewValue)

    self.textChanged.emit("Hello")

class Tool(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent = None):

        # Setting-ip UI
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

        # Button Action
        self.runButton.clicked.connect(self.onStart)

        # Progress Bar and Label. At the begining, the bar is at 0
        self.progressBar.setValue(0)
        self.progressBar.setRange(0,100)
        self.resultLabel.setText("Waiting...")  

        ####################################
        #TEST: Thread for progress bar
        self.myLongTask = TaskThread()
        self.myLongTask.cpuValueChanged.connect(self.progressBar.setValue)
        self.myLongTask.textChanged.connect(self.outputText.setText)
        self.myLongTask.taskFinished.connect(self.onFinished)
        ####################################        

    @QtCore.pyqtSlot()
    def onStart(self):    
        self.progressBar.reset()
        self.resultLabel.setText("In progress...")        
        print("(onStart)In progress mode executed")

        print("(onStart)INITIALIZE THREAD")
        self.myLongTask.start()     
        print("(onStart)THREAD EXECUTED")

        self.myLongTask.start_dt = self.startTime.dateTime() # <----
        self.myLongTask.end_dt = self.endTime.dateTime()     # <----     

    @QtCore.pyqtSlot()
    def onFinished(self):
        # Stop the pulsation when the thread has finished
        print("(onFinished) executed")
        self.progressBar.setRange(0,1)
        self.progressBar.setValue(1)
        self.resultLabel.setText("Done")        

class TaskThread(QtCore.QThread):
    cpuValueChanged = QtCore.pyqtSignal(int)  
    taskFinished = QtCore.pyqtSignal()
    textChanged = QtCore.pyqtSignal(str)

    def __del__(self):
        self.wait()

    def run(self):  
        # First, we read the times from the QDateTime elements in the interface      
        print("Getting times...")
        start_datetime_unix, end_datetime_unix = time_converter_to_unix(self.start_dt, self.end_dt)

        # Then, we put these values in my_function
        print("Executing function...")
        dummy_function(self, start_datetime_unix, end_datetime_unix) 

        # To finish, we execute onFinished.
        print("Finishing thread...")
        self.taskFinished.emit()

def main():
    app = QtGui.QApplication(sys.argv)
    window = Tool()
    window.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Here是小矮人

1 个答案:

答案 0 :(得分:0)

使用此代码仅接受CSV文件:

在您的HTML中使用:

<input type="file" id="file1" name="file" onchange="angular.element(this).scope().getFiles(this)" multiple/>

然后在您的控制器中使用:

$scope.getFiles = function ($files) {
var validExts = new Array(".csv");
var fileExt = $files.value;
fileExt = fileExt.substring(fileExt.lastIndexOf('.'));
if (validExts.indexOf(fileExt) < 0) {
  alert("Invalid file selected");
  $scope.flag=false;
}
else $scope.flag=true;
}

使用此代码清除所选文件:

document.getElementById("file1").value = "";