Inherits istd::IPolymorphic.
Interface for file upload and download operations via URLs.
IFileTransfer provides a simple contract for transferring files to and from remote servers using URL-based addressing. This interface abstracts the underlying transfer protocol (HTTP, FTP, etc.) and provides a uniform API for file operations.
Key Features
Bidirectional Transfer:
- UploadFile(): Send local file to remote server
- DownloadFile(): Retrieve remote file to local filesystem
- URL-based addressing for flexibility
Protocol Agnostic:
- Supports HTTP/HTTPS (typical implementation)
- Potentially FTP, SFTP, or custom protocols
- Protocol determined by URL scheme (http://, ftp://, etc.)
Synchronous Operations:
- Blocking calls that complete before returning
- Return value indicates success/failure
- Use in separate thread if non-blocking behavior required
Implementation
Primary Implementation:
- imthttp::CHttpFileTransfer: HTTP/HTTPS-based file transfer
- Supports authentication via IServerConnectionInterface
- Handles multipart/form-data encoding for uploads
- Manages progress tracking and error handling
Usage Examples
File Upload
QString localFile = "/path/to/document.pdf";
QUrl uploadUrl("https://example.com/api/upload");
qDebug() << "File uploaded successfully";
} else {
qCritical() << "Upload failed";
}
Interface for file upload and download operations via URLs.
virtual bool UploadFile(const QString &filePath, const QUrl &url) const =0
Upload a local file to a remote URL.
File Download
QString localPath = "/tmp/downloaded-report.pdf";
QUrl downloadUrl("https://example.com/api/files/report-2024.pdf");
qDebug() << "File downloaded to:" << localPath;
QFile::exists(localPath);
} else {
qCritical() << "Download failed";
}
virtual bool DownloadFile(const QString &filePath, const QUrl &url) const =0
Download a remote file to a local path.
Document Management System
class DocumentManager : public QObject {
public:
: m_transfer(transfer), m_baseUrl(baseUrl) {}
bool UploadDocument(const QString& localPath, const QString& documentId) {
QUrl uploadUrl = m_baseUrl;
uploadUrl.setPath(QString("/documents/%1").arg(documentId));
qInfo() << "Uploading document" << documentId << "from" << localPath;
if (m_transfer->UploadFile(localPath, uploadUrl)) {
qInfo() << "Document uploaded successfully";
emit documentUploaded(documentId);
return true;
} else {
qCritical() << "Failed to upload document";
emit uploadFailed(documentId);
return false;
}
}
bool DownloadDocument(const QString& documentId, const QString& localPath) {
QUrl downloadUrl = m_baseUrl;
downloadUrl.setPath(QString("/documents/%1").arg(documentId));
qInfo() << "Downloading document" << documentId << "to" << localPath;
if (m_transfer->DownloadFile(localPath, downloadUrl)) {
qInfo() << "Document downloaded successfully";
emit documentDownloaded(documentId, localPath);
return true;
} else {
qCritical() << "Failed to download document";
emit downloadFailed(documentId);
return false;
}
}
signals:
void documentUploaded(const QString& documentId);
void documentDownloaded(const QString& documentId, const QString& path);
void uploadFailed(const QString& documentId);
void downloadFailed(const QString& documentId);
private:
IFileTransfer* m_transfer;
QUrl m_baseUrl;
};
Best Practices
- Error Handling: Always check return value and log failures
- Network errors, permission issues, disk full, etc.
- Provide user feedback for failed operations
- File Validation: Verify file exists before upload
if (!QFile::exists(localPath)) {
qCritical() << "File not found:" << localPath;
return false;
}
- URL Construction: Use QUrl for proper URL encoding
QUrl url("https://example.com/api/upload");
url.setQuery("token=abc123");
- Path Validation: Ensure local paths are writable for downloads
QFileInfo info(localPath);
if (!info.dir().exists()) {
info.dir().mkpath(".");
}
- Large Files: Consider using separate thread for blocking operations
QtConcurrent::run([transfer, localPath, url]() {
});
- Authentication: Ensure URL includes auth tokens or use authenticated client
Limitations
- Synchronous (blocking) operations - may hang UI if called on main thread
- No built-in progress reporting - implement separately if needed
- No resume capability for interrupted transfers
- No multi-part upload support in interface (implementation-specific)
Integration Points
Implementations:
- imthttp::CHttpFileTransfer: HTTP/HTTPS file transfer
Related Components:
- IServerConnectionInterface: Provides server URL configuration
- imtservergql: GraphQL endpoints for file metadata
- imtrest: File upload/download servlets
- See also
- imthttp::CHttpFileTransfer, IServerConnectionInterface
Definition at line 202 of file IFileTransfer.h.