IntegrationFileClientUploadFileAsync 方法 (Stream, String, String, String, String, String, String, ActionInt64, Int64) |
上传数据流到服务器操作
命名空间:
HslCommunication.Enthernet
程序集:
HslCommunication (在 HslCommunication.dll 中) 版本:12.1.2.0 (12.1.2.0)
语法 public Task<OperateResult> UploadFileAsync(
Stream stream,
string serverName,
string factory,
string group,
string id,
string fileTag,
string fileUpload,
Action<long, long> processReport
)
Public Function UploadFileAsync (
stream As Stream,
serverName As String,
factory As String,
group As String,
id As String,
fileTag As String,
fileUpload As String,
processReport As Action(Of Long, Long)
) As Task(Of OperateResult)
public:
Task<OperateResult^>^ UploadFileAsync(
Stream^ stream,
String^ serverName,
String^ factory,
String^ group,
String^ id,
String^ fileTag,
String^ fileUpload,
Action<long long, long long>^ processReport
)
member UploadFileAsync :
stream : Stream *
serverName : string *
factory : string *
group : string *
id : string *
fileTag : string *
fileUpload : string *
processReport : Action<int64, int64> -> Task<OperateResult>
参数
- stream
- 类型:System.IOStream
数据流内容 - serverName
- 类型:SystemString
服务器存储的文件名称,带后缀 - factory
- 类型:SystemString
第一大类 - group
- 类型:SystemString
第二大类 - id
- 类型:SystemString
第三大类 - fileTag
- 类型:SystemString
文件的额外描述 - fileUpload
- 类型:SystemString
文件的上传人 - processReport
- 类型:SystemActionInt64, Int64
上传的进度报告
返回值
类型:
TaskOperateResult是否成功的结果对象
备注
用于分类的参数
factory,
group,
id中间不需要的可以为空,对应的是服务器上的路径系统。
警告: |
---|
失败的原因大多数来自于网络的接收异常,或是客户端不存在文件。
|
示例
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;
}
button3.Enabled = false;
string fileName = textBox3.Text;
System.IO.FileInfo fileInfo = new System.IO.FileInfo( fileName );
DateTime uploadStartTime = DateTime.Now;
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
);
button3.Enabled = true;
if (result.IsSuccess)
{
MessageBox.Show( "文件上传成功!耗时:" + (DateTime.Now - uploadStartTime).TotalSeconds.ToString( "F1" ) + " 秒" );
}
else
{
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 );
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
);
Invoke( new Action<OperateResult>( operateResult =>
{
button3.Enabled = true;
if (result.IsSuccess)
{
MessageBox.Show( "文件上传成功!" );
}
else
{
MessageBox.Show( "文件上传失败:" + result.ToMessageShowString( ) );
}
} ), result );
}
}
private void UpdateReportProgress( long sended, long totle )
{
if (progressBar1.InvokeRequired)
{
progressBar1.Invoke( new Action<long, long>( UpdateReportProgress ), sended, totle );
return;
}
int value = (int)(sended * 100L / totle);
label10.Text = sended + "/" + totle;
progressBar1.Value = value;
}
参见