点击或拖拽改变大小

IntegrationFileClientDownloadFile 方法 (String, String, String, String, ActionInt64, Int64, Stream)

下载服务器的文件到本地的文件操作,需要指定下载的文件的名字,三级分类信息,本次保存的文件名,支持进度报告。
To download a file from the server to a local file, you need to specify the name of the downloaded file, the three-level classification information, the name of the file saved this time, and support for progress reports.

命名空间:  HslCommunication.Enthernet
程序集:  HslCommunication (在 HslCommunication.dll 中) 版本:11.8.2.0 (11.8.2.0)
语法
public OperateResult DownloadFile(
	string fileName,
	string factory,
	string group,
	string id,
	Action<long, long> processReport,
	Stream stream
)

参数

fileName
类型:SystemString
文件名称,带后缀
factory
类型:SystemString
第一大类
group
类型:SystemString
第二大类
id
类型:SystemString
第三大类
processReport
类型:SystemActionInt64, Int64
下载的进度报告,第一个数据是已完成总接字节数,第二个数据是总字节数。
stream
类型:System.IOStream

[缺少 "M:HslCommunication.Enthernet.IntegrationFileClient.DownloadFile(System.String,System.String,System.String,System.String,System.Action{System.Int64,System.Int64},System.IO.Stream)" 的 <param name="stream"/> 文档]

返回值

类型:OperateResult
是否成功的结果对象
备注
用于分类的参数factorygroupid中间不需要的可以为空,对应的是服务器上的路径系统。

警告 警告:
失败的原因大多数来自于网络的接收异常,或是服务器不存在文件。
示例
DownloadFile示例
/*************************************************************************************************
 * 
 *   一条指令即可完成文件的下载操作,下载模式有三种
 *   1. 指定需要下载的文件名(带后缀)
 *   2. 将服务器上的数据下载到流(stream)中
 *   3. 将服务器上的数据下载到bitmap图片中
 * 
 ********************************************************************************************/

private async void button4_Click( object sender, EventArgs e )
{
    // 点击开始下载,此处按照实际项目需求放到了后台线程处理,事实上这种耗时的操作就应该放到后台线程
    // click this button to start a backgroud thread for downloading file
    progressBar2.Value = 0;
    button4.Enabled = false;

    string fileName = textBox_download_fileName.Text;
    DateTime downloadStartTime = DateTime.Now;
    OperateResult result = await integrationFileClient.DownloadFileAsync(
            fileName,                                              // 文件在服务器上保存的名称,举例123.txt
            textBox_download_factory.Text,                         // 第一级分类,指示文件存储的类别,对应在服务器端存储的路径不一致
            textBox_download_group.Text,                           // 第二级分类,指示文件存储的类别,对应在服务器端存储的路径不一致
            textBox_download_id.Text,                              // 第三级分类,指示文件存储的类别,对应在服务器端存储的路径不一致
            DownloadReportProgress,                                // 文件下载的时候的进度报告,友好的提示下载进度信息
            Application.StartupPath + @"\Files\" + fileName        // 下载后在文本保存的路径,也可以直接下载到 MemoryStream 的数据流中,或是bitmap中,或是手动选择存储路径
            );

    button4.Enabled = true;
    if (result.IsSuccess)
    {
        // message: file download success
        MessageBox.Show( "文件下载成功!耗时:" + (DateTime.Now - downloadStartTime).TotalSeconds.ToString( "F1" ) + " 秒" );
    }
    else
    {
        // 失败原因多半来自网络异常,还有文件不存在,分类名称填写异常
        // mostly failed by network exception, like offline, and file not exsist,
        MessageBox.Show( "文件下载失败:" + result.ToMessageShowString( ) );
    }
}

private void button10_Click( object sender, EventArgs e )
{
    string fileName = textBox_download_fileName.Text;
    OperateResult<bool> result = integrationFileClient.IsFileExists(
            fileName,                                              // 文件在服务器上保存的名称,举例123.txt
            textBox_download_factory.Text,                         // 第一级分类,指示文件存储的类别,对应在服务器端存储的路径不一致
            textBox_download_group.Text,                           // 第二级分类,指示文件存储的类别,对应在服务器端存储的路径不一致
            textBox_download_id.Text                               // 第三级分类,指示文件存储的类别,对应在服务器端存储的路径不一致
            );
    if (result.IsSuccess)
    {
        if (result.Content)
            MessageBox.Show( "文件存在!" );
        else
            MessageBox.Show( "文件不存在!" );
    }
    else
    {
        MessageBox.Show( result.Message );
    }
}

private void ThreadDownloadFile( object filename )
{
    if (filename is string fileName)
    {
        OperateResult result = integrationFileClient.DownloadFile(
            fileName,                                              // 文件在服务器上保存的名称,举例123.txt
            textBox_download_factory.Text,                         // 第一级分类,指示文件存储的类别,对应在服务器端存储的路径不一致
            textBox_download_group.Text,                           // 第二级分类,指示文件存储的类别,对应在服务器端存储的路径不一致
            textBox_download_id.Text,                              // 第三级分类,指示文件存储的类别,对应在服务器端存储的路径不一致
            DownloadReportProgress,                                // 文件下载的时候的进度报告,友好的提示下载进度信息
            Application.StartupPath + @"\Files\" + filename        // 下载后在文本保存的路径,也可以直接下载到 MemoryStream 的数据流中,或是bitmap中,或是手动选择存储路径
            );

        // 切换到UI前台显示结果
        Invoke( new Action<OperateResult>( operateResult =>
        {
            button4.Enabled = true;
            if (result.IsSuccess)
            {
                // message: file download success
                MessageBox.Show( "文件下载成功!" );
            }
            else
            {
                // 失败原因多半来自网络异常,还有文件不存在,分类名称填写异常
                // mostly failed by network exception, like offline, and file not exsist,
                MessageBox.Show( "文件下载失败:" + result.ToMessageShowString( ) );
            }
        } ), result );
    }
}

/// <summary>
/// 用于更新文件下载进度的方法,该方法是线程安全的
/// </summary>
/// <param name="receive">已经接收的字节数</param>
/// <param name="totle">总字节数</param>
private void DownloadReportProgress( long receive, long totle )
{
    if (progressBar2.InvokeRequired)
    {
        progressBar2.Invoke( new Action<long, long>( DownloadReportProgress ), receive, totle );
        return;
    }


    // 此处代码是线程安全的
    // thread-safe code
    int value = (int)(receive * 100L / totle);
    progressBar2.Value = value;
    label9.Text = receive + "/" + totle;
}
参见