Exemplo n.º 1
0
        //上传文件
        private string upSingleFile(HttpPostedFile file, byte typeStr, LoginUser loginUser, long DevId)
        {
            if (!SystemService.isConnect)
            {
                return("no|与通讯服务器连接中断!");
            }
            Device device = DeviceModule.GetDeviceByID(DevId);

            if (device == null)
            {
                return("no|要操作的设备不存在!");
            }

            string DeviceNo = DeviceModule.GetFullDeviceNoByID(device.Id);

            string infos  = "";
            bool   fileOK = false;
            string fileExtension;
            string fileName = System.IO.Path.GetFileName(file.FileName);

            if (fileName != "")
            {
                if (file.ContentLength >= 1024 * 1024 * 1)
                {
                    infos = "no|文件太大,目前仅支持1M以内的文档!";
                }
                else
                {
                    string theFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + DeviceNo + "_" + System.IO.Path.GetFileNameWithoutExtension(fileName);
                    if (typeStr == 0x01)
                    {
                        theFileName += ".jpg";
                    }
                    else if (typeStr == 0x02)
                    {
                        theFileName += ".bin";
                    }

                    fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
                    //String[] allowedExtensions = { ".jpg", ".jpeg", ".gif", ".bmp", ".png", ".icon" };
                    String[] allowedExtensions = { };
                    if (allowedExtensions.Length > 0)
                    {
                        for (int i = 0; i < allowedExtensions.Length; i++)
                        {
                            if (fileExtension == allowedExtensions[i])
                            {
                                fileOK = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        fileOK = true;
                    }

                    if (!fileOK)
                    {
                        infos = "no|不支持上传此类型文件!目前支持的图片格式有:" + String.Join("|", allowedExtensions);
                    }
                    else
                    {
                        /**********start update by kqz 2017-3-8*****************/
                        //int packetSize = 230;
                        int packetSize = 256;
                        /**********end update by kqz 2017-3-8 20:27****************/
                        string        path = System.Web.HttpContext.Current.Request.MapPath("~/UploadFiles/User/" + loginUser.LoginName + "/");
                        DirectoryInfo dir  = new DirectoryInfo(path);
                        if (!dir.Exists)
                        {
                            dir.Create();
                        }
                        file.SaveAs(path + theFileName);

                        byte[] bsOld = new byte[file.ContentLength];
                        file.InputStream.Read(bsOld, 0, bsOld.Length);
                        file.InputStream.Seek(0, SeekOrigin.Begin);
                        //start update by kqz 2017-3-20
                        int count = 0;
                        //Debug.Print("len =========" + file.ContentLength);
                        //int count = file.ContentLength / packetSize + 1;
                        if ((file.ContentLength % packetSize) == 0)
                        {
                            count = file.ContentLength / packetSize;
                        }
                        else
                        {
                            count = file.ContentLength / packetSize + 1;
                        }
                        //end update by kqz 2017-3-20
                        //byte[] bsNew = new byte[file.ContentLength];

                        for (int i = 0; i < count; i++)
                        {
                            #region 发送数据
                            try
                            {
                                byte[] bs;
                                if (i != count - 1)
                                {
                                    bs = new byte[packetSize];
                                }
                                else
                                {
                                    bs = new byte[file.ContentLength - packetSize * i];
                                }

                                if (i == 0)
                                {
                                    bs    = new byte[packetSize + 1];
                                    bs[0] = typeStr;
                                    Array.Copy(bsOld, 0, bs, 1, bs.Length - 1);
                                    //update by kqz 2017-3-20
                                    //Array.Copy(bsOld, 0, bs, 1, bs.Length - 1);
                                    //Array.Copy(bs, 1, bsNew, 0, bs.Length - 1);
                                }
                                else
                                {
                                    Array.Copy(bsOld, packetSize * i, bs, 0, bs.Length);
                                    //}
                                    //Array.Copy(bsOld, packetSize * i - 1, bs, 0, bs.Length);
                                    //Array.Copy(bs, 0, bsNew, packetSize * i - 1, bs.Length);
                                }
                                Debug.Print("aaaaaaaaaaaaaaaaaaaaaa------------------\r\n");
                                CmdToDtuSendFile cmd = new CmdToDtuSendFile();
                                cmd.AddressField = DeviceNo.Substring(0, 12) + Convert.ToInt32(DeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                                cmd.StationType  = (byte)device.StationType;
                                cmd.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                                cmd.Sum          = (short)count;
                                cmd.Curr         = (short)(i + 1);
                                cmd.Content      = bs;
                                cmd.RawDataChar  = cmd.WriteMsg();
                                cmd.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd.RawDataChar);

                                DeviceOperation op = new DeviceOperation();
                                op.DeviceNo      = DeviceNo;
                                op.DeviceName    = device.DeviceName;
                                op.OperationTime = DateTime.Now;
                                op.OperationType = "中心下发文件";
                                op.RawData       = cmd.RawDataStr;
                                op.Remark        = cmd.Sum + "|" + cmd.Curr;
                                op.UserId        = loginUser.UserId;
                                op.UserName      = SysUserModule.GetUser(loginUser.UserId).UserName;
                                op.State         = "等待发送";

                                myLogger.Info(op.OperationType + ":" + cmd.RawDataStr);
                                byte[] cmd_send = cmd.RawDataChar;
                                myLogger.Error((i + 1).ToString().PadLeft(count.ToString().Length, '0') + ":" + cmd.RawDataStr);

                                ResMsg resMsg = SendCmd(cmd_send);
                                #region 判断结果
                                if (resMsg.Result)
                                {
                                    if (resMsg.Message == BaseProtocol.DeviceOffline)
                                    {
                                        infos = "no|查询终端不在线!";
                                    }
                                    else
                                    {
                                        byte[]      cmd_receive = HexStringUtility.HexStringToByteArray(resMsg.Message);
                                        BaseMessage message     = new BaseMessage();
                                        message.RawDataChar = cmd_receive;
                                        string msg = message.ReadMsg();
                                        //myLogger.Error("------------------the id is ========= +" +cmd_receive[19] +"  ff \r\n");
                                        //if (cmd_receive[19] == 0x00)
                                        //{
                                        // op.State = "发送失败";
                                        // infos = "no|" + "终端正忙,请稍候传输数据!";
                                        //return "发送失败,终端正忙,请稍候传输数据!";
                                        // }
                                        if (msg == "")
                                        {
                                            CmdResponseToDtuSendFile res = new CmdResponseToDtuSendFile(message);
                                            string msg1 = res.ReadMsg();
                                            if (msg1 == "")
                                            {
                                                if (res.Result == 1)
                                                {
                                                    op.State = "发送成功";
                                                    //infos = "yes|结果为1,继续";
                                                }
                                                else
                                                {
                                                    op.State = "发送成功";
                                                    infos    = "no|结果为0,退出";
                                                }
                                            }
                                            else
                                            {
                                                op.State = "发送失败";
                                                infos    = "no|" + msg1;
                                            }
                                        }
                                        else
                                        {
                                            op.State = "发送失败";
                                            infos    = "no|" + msg;
                                        }

                                        if (cmd_receive[19] == 0x00)
                                        {
                                            op.State = "发送失败";
                                            infos    = "no|" + "终端正忙,请稍候传输数据!";
                                            //return "发送失败,终端正忙,请稍候传输数据!";
                                        }

                                        if (i == count - 1)
                                        {
                                            op.State = "发送成功";
                                            infos    = "no|结果为0,退出";
                                        }
                                    }
                                }
                                else
                                {
                                    return("发送失败");
                                }
                                #endregion
                                if (infos.StartsWith("no|"))
                                {
                                    op.State += "|" + infos.Split('|')[1];
                                }
                                DeviceOperationModule.AddDeviceOperation(op);
                                if (op.State.Contains("发送失败"))
                                {
                                    myLogger.Error((i + 1).ToString().PadLeft(count.ToString().Length, '0') + "-" + count + ":" + infos);
                                    return(infos);
                                }
                            }
                            catch (Exception e)
                            {
                                myLogger.Error((i + 1).ToString().PadLeft(count.ToString().Length, '0') + "-" + count + ":出错,发送失败" + e.Message + "  the trace :" + e.StackTrace);
                                //return "no|" + (i + 1);
                            }
                            #endregion
                        }


                        infos = "yes|" + theFileName.Split('_')[2];
                    }
                }
            }
            else
            {
                infos = "no|未选择文件!";
            }
            return(infos);
        }
Exemplo n.º 2
0
        private void downloadApp()
        {
            if (sr == null)
            {
                return;
            }
            byte[] buffer = new byte[packetSize];
            int    read   = sr.Read(buffer, 0, packetSize);

            //MessageBox.Show("read====" + read);
            if (read > 0)
            {
                readCount += read;
                //还要更新进度条控件
                ValueEventArgs e = new ValueEventArgs();
                e.Value = read;
                downloadWorker.onValueChanged(e);
                byte[] sendBuffer;
                //port.sendProtocol(sendBuf, sendBuf.Length);
                if (isFirstSend == true)
                {
                    sendBuffer    = new byte[1 + packetSize];
                    sendBuffer[0] = fileType;
                    Array.Copy(buffer, 0, sendBuffer, 1, packetSize);
                }
                else
                {
                    //Debug.WriteLine("the read is \r\n" + read);
                    sendBuffer = new byte[read];
                    Array.Copy(buffer, 0, sendBuffer, 0, read);
                    // sendBuffer = buffer;
                }
                string           DeviceNo = DeviceModule.GetFullDeviceNoByID(device.Id);
                CmdToDtuSendFile cmd      = new CmdToDtuSendFile();
                cmd.AddressField = DeviceNo.Substring(0, 12) + Convert.ToInt32(DeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                cmd.StationType  = (byte)device.StationType;
                cmd.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                cmd.Sum          = (short)count;
                cmd.Curr         = (short)(index + 1);
                cmd.Content      = sendBuffer;
                cmd.RawDataChar  = cmd.WriteMsg();
                cmd.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd.RawDataChar);

                byte[] cmd_send = cmd.RawDataChar;
                client.send(cmd_send, 0, cmd_send.Length);
                index++;
                this.timer1.Start();
                this.currentFrameLabel.Text = index.ToString();
            }
            else
            {
                readCount = 0;
                sr.Close();
                sr = null;

                MessageBox.Show("下载成功!");
                //将申请的资源释放掉
                this.fileTxt.Text       = "";
                this.progressBar1.Value = 0;
                count = 0;
                index = 0;
                this.totalFrameLabel.Text   = "0";
                this.currentFrameLabel.Text = "0";
                this.timer1.Stop();
                this.downloadBtn.Enabled = true;
            }
        }