Ejemplo n.º 1
0
        /// <summary>
        /// 文件流异步上传,返回文件guid
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public object Post(AsyncsFileUploadRequest request)
        {
            int code = (int)FS_ErrorCode.Request_Success;
            if (base.Request.Files.Length == 0)
            {
                LogUtil.Info(" request.file is null");
                code = (int)FS_ErrorCode.RequestFile_Null;
                return new AsyncsFileUploadResponse { ret = code, message = ErrorCodeDic.GetInstance().CodeMessage(code), File_guid = string.Empty };
            }

            AsyncsFileUploadResponse response = new AsyncsFileUploadResponse();

            try
            {
                ICommonFilesService fileService = new CommonFilesService();
                string fileName = (string.IsNullOrEmpty(request.file_name)) ? base.Request.Files[0].FileName : request.file_name;
                byte[] bytes = streamToBytes(base.Request.Files[0].InputStream);

                code = RequestValid(request, bytes);

                if (code != (int)FS_ErrorCode.Request_Success)
                    return new AsyncsFileUploadResponse { ret = code, message = ErrorCodeDic.GetInstance().CodeMessage(code), File_guid = string.Empty };

                response = fileService.AsynFileUpload(fileName, bytes);
            }
            catch (Exception e)
            {
                string err = string.Format("CommonFileServiceHost.AsyncsFileUploadRequest error = {0}, request = {1}", e.ToString(), JsonUtil<AsyncsFileUploadRequest>.ToJson(request));
                LogUtil.Error(err);
            }

            return response;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 文件流异步上传有效性校验
        /// </summary>
        /// <param name="request"></param>
        /// <param name="bytes"></param>
        /// <returns></returns>
        private int RequestValid(AsyncsFileUploadRequest request, byte[] bytes)
        {
            int errorCode = (int)FS_ErrorCode.Request_Success;

            if (base.Request.Files.Length == 0)
            {
                LogUtil.Info("Error: request.file is null");
                errorCode = (int)FS_ErrorCode.RequestFile_Null;
            }
            if (request.file_size == 0 || base.Request.FormData["file_size"] != bytes.Length.ToString())
            {
                LogUtil.Info("Error: request file is not valid, please check and retry.");
                errorCode = (int)FS_ErrorCode.RequestFile_Size_Invalid;
            }

            return errorCode;
        }