コード例 #1
0
ファイル: BigAttachManager.cs プロジェクト: caodevelop/bgqt
        public bool QuickUpload(
            Guid transactionid,
            Guid userid,
            UploadFileItemInfo info,
            out string strJsonResult)
        {
            bool result = true;

            strJsonResult = string.Empty;
            ErrorCodeInfo error    = new ErrorCodeInfo();
            string        paramstr = string.Empty;

            paramstr += $"userid:{userid}";
            paramstr += $"||FileName:{info.FileName}";
            paramstr += $"||HashCode:{info.HashCode}";
            string funname = "QuickUpload";

            try
            {
                do
                {
                    AttachResultInfo    resultinfo = new AttachResultInfo();
                    BigAttachDBProvider Provider   = new BigAttachDBProvider();
                    result = Provider.QuickUpload(transactionid, userid, ref info, out error);
                    if (result == true)
                    {
                        resultinfo.data = info;
                        strJsonResult   = JsonConvert.SerializeObject(resultinfo);
                        LoggerHelper.Info(userid.ToString(), funname, paramstr, Convert.ToString(error.Code), true, transactionid);
                        result = true;
                        break;
                    }
                    else
                    {
                        LoggerHelper.Info(userid.ToString(), funname, paramstr, Convert.ToString(error.Code), false, transactionid);
                        result = false;
                    }
                } while (false);
            }
            catch (Exception ex)
            {
                error.Code = ErrorCode.Exception;
                LoggerHelper.Info(userid.ToString(), funname, paramstr, Convert.ToString(error.Code), false, transactionid);
                LoggerHelper.Error($"BigAttachManager调用{funname}异常", paramstr, ex.ToString(), transactionid);
                strJsonResult = JsonHelper.ReturnJson(false, Convert.ToInt32(error.Code), error.Info);
                result        = false;
            }
            return(result);
        }
コード例 #2
0
ファイル: File.ashx.cs プロジェクト: caodevelop/bgqt
        private string QuickUpload(HttpContext context)
        {
            string        strJsonResult = string.Empty;
            string        userAccount   = string.Empty;
            ErrorCodeInfo error         = new ErrorCodeInfo();
            Guid          transactionid = Guid.NewGuid();
            string        funname       = "QuickUpload";

            try
            {
                do
                {
                    string filename = context.Request.Form["FileName"];
                    string hashcode = context.Request.Form["HashCode"];

                    UploadFileItemInfo info = new UploadFileItemInfo();
                    info.FileName = filename;
                    info.HashCode = hashcode;

                    Guid userid = this.CheckCookie(context);
                    if (userid == Guid.Empty)
                    {
                        //error?
                        break;
                    }
                    BigAttachManager dll = new BigAttachManager(ClientIP);
                    dll.QuickUpload(transactionid, userid, info, out strJsonResult);
                } while (false);
            }
            catch (Exception ex)
            {
                error.Code = ErrorCode.Exception;
                LoggerHelper.Error($"File.ashx调用接口{funname}异常", context.Request.RawUrl, ex.ToString(), transactionid);
                LoggerHelper.Info(userAccount, funname, context.Request.RawUrl, Convert.ToString(error.Code), false, transactionid);
                strJsonResult = JsonHelper.ReturnJson(false, Convert.ToInt32(error.Code), error.Info);
            }

            return(strJsonResult);
        }
コード例 #3
0
ファイル: BigAttachManager.cs プロジェクト: caodevelop/bgqt
        public bool Upload(
            Guid transactionid,
            Guid userid,
            UploadFileItemInfo info,
            out string strJsonResult)
        {
            bool result = true;

            strJsonResult = string.Empty;
            ErrorCodeInfo error = new ErrorCodeInfo();

            AttachResultInfo resultinfo = new AttachResultInfo();
            string           paramstr   = string.Empty;

            paramstr += $"userid:{userid}";
            paramstr += $"||FileName:{info.FileName}";
            paramstr += $"||HashCode:{info.HashCode}";
            paramstr += $"||TempID:{info.TempID}";
            paramstr += $"||TotalChunks:{info.TotalChunks}";
            paramstr += $"||ChunkIndex:{info.ChunkIndex}";
            paramstr += $"||Position:{info.Position}";
            paramstr += $"||FileSizeInt:{info.FileSizeInt}";
            string funname = "Upload";

            try
            {
                do
                {
                    BigAttachDBProvider Provider = new BigAttachDBProvider();
                    UploadFileItemInfo  infonow  = new UploadFileItemInfo();
                    infonow.TempID     = info.TempID;
                    infonow.FileName   = info.FileName;
                    infonow.FileSize   = info.FileSizeInt;
                    infonow.ChunkIndex = info.ChunkIndex;
                    infonow.HashCode   = info.HashCode;

                    if (info.FileName.IndexOf('.') > 0)
                    {
                        infonow.ExtensionName = info.FileName.Substring(info.FileName.LastIndexOf('.'));
                    }

                    result = Provider.CheckFile(transactionid, userid, ref infonow, out error);
                    if (result == false)
                    {
                        //error
                        //?
                        break;
                    }

                    //check file exists and create
                    FileInfo finfo = new FileInfo(infonow.FilePath);
                    if (finfo.Exists == false && infonow.ChunkIndex != 0)
                    {
                        //error
                        //?
                        break;
                    }
                    if (finfo.Exists == false && infonow.ChunkIndex == 0)
                    {
                        FileStream createfs = finfo.Create();
                        createfs.Close();
                    }
                    finfo.Refresh();

                    long sizenow = finfo.Length;

                    FileStream fs = null;
                    try
                    {
                        if (sizenow < info.Position)
                        {
                            ErrorResult er = new ErrorResult();
                            er.ErrorCode    = "3003";
                            er.ChunkIndex   = infonow.ChunkIndex;
                            resultinfo.data = info;
                            strJsonResult   = JsonConvert.SerializeObject(resultinfo);
                            result          = true;
                            //delete file
                            //finfo.Delete();
                            //BigFileItemInfo deleteinfo = new BigFileItemInfo();
                            //deleteinfo.TempID = infonow.TempID;
                            //Provider.CancelUpload(transactionid, userid, ref deleteinfo, out error);
                            //error
                            // to do
                            break;
                        }
                        else if (sizenow > info.Position)
                        {
                            ErrorResult er = new ErrorResult();
                            er.ErrorCode    = "3003";
                            er.ChunkIndex   = infonow.ChunkIndex;
                            resultinfo.data = info;
                            strJsonResult   = JsonConvert.SerializeObject(resultinfo);
                            result          = true;
                        }
                        else
                        {
                            fs = new FileStream(infonow.FilePath, FileMode.Append, FileAccess.Write, FileShare.Read, 1024);

                            fs.Write(info.Data, 0, info.Data.Length);
                            fs.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        LoggerHelper.Error($"BigAttachManager调用{funname}异常", paramstr, ex.ToString(), transactionid);
                    }
                    finally
                    {
                        if (fs != null)
                        {
                            fs.Close();
                        }
                    }

                    finfo.Refresh();
                    info.TempID = infonow.TempID;
                    if (finfo.Length == infonow.FileSizeInt)
                    {
                        // upload finish
                        // move file to new folder
                        string newpath = infonow.FilePath.Substring(0, infonow.FilePath.LastIndexOf("\\temp\\")) +
                                         @"\" + DateTime.Now.ToString("yyyy-MM-dd");
                        string newfilepath = newpath + infonow.FilePath.Substring(infonow.FilePath.LastIndexOf('\\'));
                        if (Directory.Exists(newpath) == false)
                        {
                            Directory.CreateDirectory(newpath);
                        }
                        finfo.MoveTo(newfilepath);
                        info.FilePath = newfilepath;
                        result        = Provider.UploadFinish(transactionid, userid, ref info, out error);
                        if (result == true)
                        {
                            info.Succeed    = true;
                            resultinfo.data = info;
                            strJsonResult   = JsonConvert.SerializeObject(resultinfo);
                            //LoggerHelper.Info(userid.ToString(), funname, paramstr, Convert.ToString(error.Code), true, transactionid);

                            result = true;
                            break;
                        }
                    }
                    else
                    {
                        result = Provider.Upload(transactionid, userid, ref info, out error);
                        if (result == true)
                        {
                            info.Succeed    = false;
                            resultinfo.data = info;
                            strJsonResult   = JsonConvert.SerializeObject(resultinfo);
                            result          = true;
                            break;
                        }
                    }
                } while (false);
            }
            catch (Exception ex)
            {
                error.Code = ErrorCode.Exception;
                LoggerHelper.Info(userid.ToString(), funname, paramstr, Convert.ToString(error.Code), false, transactionid);
                LoggerHelper.Error($"BigAttachManager调用{funname}异常", paramstr, ex.ToString(), transactionid);
                strJsonResult = JsonHelper.ReturnJson(false, Convert.ToInt32(error.Code), error.Info);
                result        = false;
            }
            return(result);
        }
コード例 #4
0
ファイル: File.ashx.cs プロジェクト: caodevelop/bgqt
        private string Upload(HttpContext context)
        {
            string        strJsonResult = string.Empty;
            string        userAccount   = string.Empty;
            ErrorCodeInfo error         = new ErrorCodeInfo();
            Guid          transactionid = Guid.NewGuid();
            string        funname       = "Upload";

            try
            {
                do
                {
                    Guid userid = this.CheckCookie(context);
                    if (userid == Guid.Empty)
                    {
                        //error?
                        break;
                    }
                    if (context.Request.Files.Count <= 0)
                    {
                        //error?
                        break;
                    }

                    HttpPostedFile _file  = context.Request.Files[0];
                    Stream         stream = _file.InputStream;
                    if (stream.Length <= 0)
                    {
                        //error?
                        break;
                    }
                    byte[] filedata = new byte[stream.Length];
                    stream.Read(filedata, 0, filedata.Length);



                    string             tempid      = context.Request.Form["Identifier"];
                    string             HashCode    = context.Request.Form["HashCode"];
                    string             TotalChunks = context.Request.Form["TotalChunks"];
                    string             ChunkIndex  = context.Request.Form["ChunkIndex"];
                    string             FileName    = context.Request.Form["FileName"];
                    string             FileSize    = context.Request.Form["FileSize"];
                    string             Position    = context.Request.Form["Position"];
                    UploadFileItemInfo info        = new UploadFileItemInfo();
                    info.TempID      = Guid.Parse(tempid);
                    info.TotalChunks = Convert.ToInt32(TotalChunks);
                    info.ChunkIndex  = Convert.ToInt32(ChunkIndex);
                    info.FileName    = FileName;
                    info.HashCode    = HashCode;
                    info.FileSizeInt = Convert.ToInt32(FileSize);
                    info.Position    = Convert.ToInt32(Position);
                    info.Data        = filedata;
                    BigAttachManager dll = new BigAttachManager(ClientIP);
                    dll.Upload(transactionid, userid, info, out strJsonResult);
                } while (false);
            }
            catch (Exception ex)
            {
                error.Code = ErrorCode.Exception;
                LoggerHelper.Error($"File.ashx调用接口{funname}异常", context.Request.RawUrl, ex.ToString(), transactionid);
                LoggerHelper.Info(userAccount, funname, context.Request.RawUrl, Convert.ToString(error.Code), false, transactionid);
                strJsonResult = JsonHelper.ReturnJson(false, Convert.ToInt32(error.Code), error.Info);
            }

            return(strJsonResult);
        }