点击或拖拽改变大小

IntegrationFileClientUploadFile 方法 (String, String, String, String, String, String, String, ActionInt64, Int64)

上传本地的文件到服务器操作,如果该文件已经存在,那么就更新这个文件。
Upload a local file to the server. If the file already exists, update the file.

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

参数

fileName
类型:SystemString
本地的完整路径的文件名称
serverName
类型:SystemString
服务器存储的文件名称,带后缀,例如123.txt
factory
类型:SystemString
第一大类
group
类型:SystemString
第二大类
id
类型:SystemString
第三大类
fileTag
类型:SystemString
文件的额外描述
fileUpload
类型:SystemString
文件的上传人
processReport
类型:SystemActionInt64, Int64
上传的进度报告

返回值

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

警告 警告:
失败的原因大多数来自于网络的接收异常,或是客户端不存在文件。
示例
UploadFile示例
/*************************************************************************************************
 * 
 *   一条指令即可完成文件的上传操作,上传模式有三种
 *   1. 指定本地的完整路径的文件名
 *   2. 将流(stream)中的数据上传到服务器
 *   3. 将bitmap图片数据上传到服务器
 * 
 ********************************************************************************************/

private void button2_Click( object sender, EventArgs e )
{
    // 选择文件
    using (OpenFileDialog ofd = new OpenFileDialog( ))
    {
        if (ofd.ShowDialog( ) == DialogResult.OK)
        {
            textBox3.Text = ofd.FileName;
        }
    }
}

private async void button3_Click( object sender, EventArgs e )
{
    // 开始上传
    if (!string.IsNullOrEmpty( textBox3.Text ))
    {
        if(!System.IO.File.Exists( textBox3.Text ))
        {
            MessageBox.Show( "选择的文件不存在,退出!" );
            return;
        }

        // 点击开始上传,此处按照实际项目需求放到了后台线程处理,事实上这种耗时的操作就应该放到后台线程
        // click the button and start a background thread to upload file
        // System.Threading.Thread thread = new System.Threading.Thread( new System.Threading.ParameterizedThreadStart( (ThreadUploadFile) ) );
        // thread.IsBackground = true;
        // thread.Start( textBox3.Text );
        // progressBar1.Value = 0;

        button3.Enabled = false;
        string fileName = textBox3.Text;
        System.IO.FileInfo fileInfo = new System.IO.FileInfo( fileName );
        DateTime uploadStartTime = DateTime.Now;
        // 开始正式上传,关于三级分类,下面只是举个例子,上传成功后去服务器端寻找文件就能明白
        // start to upload file to server , u shold specify the catgray about the file
        OperateResult result = await integrationFileClient.UploadFileAsync(
            fileName,                       // 需要上传的原文件的完整路径,上传成功还需要个条件,该文件不能被占用
            fileInfo.Name,                  // 在服务器存储的文件名,带后缀,一般设置为原文件的文件名,当然您也可以重新设置名字
            textBox_upload_factory.Text,    // 第一级分类,指示文件存储的类别,对应在服务器端存储的路径不一致
            textBox_upload_group.Text,      // 第二级分类,指示文件存储的类别,对应在服务器端存储的路径不一致
            textBox_upload_id.Text,         // 第三级分类,指示文件存储的类别,对应在服务器端存储的路径不一致
            textBox_upload_tag.Text,        // 这个文件的额外描述文本,可以为空("")
            textBox_upload_name.Text,       // 文件的上传人,当然你也可以不使用,可以为空("")
            UpdateReportProgress            // 文件上传时的进度报告,如果你不需要,指定为NULL就行,一般文件比较大,带宽比较小,都需要进度提示
            );
        button3.Enabled = true;
        if (result.IsSuccess)
        {
            // file upload success
            MessageBox.Show( "文件上传成功!耗时:" + (DateTime.Now - uploadStartTime).TotalSeconds.ToString( "F1" ) + " 秒" );
        }
        else
        {
            // 失败原因多半来自网络异常,还有文件不存在,分类名称填写异常
            // mostly failed by network exception, like offline
            MessageBox.Show( "文件上传失败:" + result.ToMessageShowString( ) );
        }
    }
    else
    {
        MessageBox.Show( "Please Select a File" );
    }
}

private void ThreadUploadFile( object filename )
{
    if (filename is string fileName)
    {
        System.IO.FileInfo fileInfo = new System.IO.FileInfo( fileName );
        // 开始正式上传,关于三级分类,下面只是举个例子,上传成功后去服务器端寻找文件就能明白
        // start to upload file to server , u shold specify the catgray about the file
        OperateResult result = integrationFileClient.UploadFile(
            fileName,                       // 需要上传的原文件的完整路径,上传成功还需要个条件,该文件不能被占用
            fileInfo.Name,                  // 在服务器存储的文件名,带后缀,一般设置为原文件的文件名,当然您也可以重新设置名字
            textBox_upload_factory.Text,    // 第一级分类,指示文件存储的类别,对应在服务器端存储的路径不一致
            textBox_upload_group.Text,      // 第二级分类,指示文件存储的类别,对应在服务器端存储的路径不一致
            textBox_upload_id.Text,         // 第三级分类,指示文件存储的类别,对应在服务器端存储的路径不一致
            textBox_upload_tag.Text,        // 这个文件的额外描述文本,可以为空("")
            textBox_upload_name.Text,       // 文件的上传人,当然你也可以不使用,可以为空("")
            UpdateReportProgress            // 文件上传时的进度报告,如果你不需要,指定为NULL就行,一般文件比较大,带宽比较小,都需要进度提示
            );

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

/// <summary>
/// 用于更新上传进度的方法,该方法是线程安全的
/// </summary>
/// <param name="sended">已经上传的字节数</param>
/// <param name="totle">总字节数</param>
private void UpdateReportProgress( long sended, long totle )
{
    if (progressBar1.InvokeRequired)
    {
        progressBar1.Invoke( new Action<long, long>( UpdateReportProgress ), sended, totle );
        return;
    }


    // 此处代码是安全的
    // thread-safe code
    int value = (int)(sended * 100L / totle);

    label10.Text = sended + "/" + totle;
    progressBar1.Value = value;
}
参见