Exemplo n.º 1
0
        private byte[] genfilename(string extInfo, byte[] req)
        {
            IDictionary <string, string> ext = DCUtil.ParaStrToDic(extInfo);

            if (!ext.ContainsKey("filecate"))
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo(),
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "请求参数filecate为空" },
                }
                           ));
            }
            string filecate = ext["filecate"];

            string filename = genFileNameInternal(filecate);

            return(PackOrb.PackRespose(
                       new HttpHeadInfo {
                StatusCode = HttpStatusCode.Succeed
            },
                       new ListDictionary {
                { "respnum", 1 },
                { "respmsg", "文件名生成成功" },
                { "filename", filename },
            }
                       ));
        }
Exemplo n.º 2
0
        private byte[] savefile(string extInfo, byte[] req)
        {
            IDictionary <string, string> ext = DCUtil.ParaStrToDic(extInfo);

            if (!ext.ContainsKey("filecate"))
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo(),
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "请求参数filecate为空" },
                }
                           ));
            }
            string filecate = ext["filecate"];

            string filename = this.genFileNameInternal(filecate);
            string filepath = this.genFilePathInternal(filename);

            try
            {
                FileStream fs = new FileStream(Path.Combine(rootDir, filepath), FileMode.Create, FileAccess.Write);
                fs.Write(req, 0, req.Length);
                fs.Close();

                return(PackOrb.PackRespose(
                           new HttpHeadInfo {
                    StatusCode = HttpStatusCode.Succeed
                },
                           new ListDictionary {
                    { "respnum", req.Length },
                    { "respmsg", "文件保存成功" },
                    { "filename", filename },
                }
                           ));
            }
            catch (Exception ex)
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo {
                    StatusCode = HttpStatusCode.ServerSideError
                },
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "文件保存异常 " + ex.Message },
                }
                           ));
            }
        }
Exemplo n.º 3
0
        private byte[] readfile(string extInfo, byte[] req)
        {
            IDictionary <string, string> ext = DCUtil.ParaStrToDic(extInfo);

            if (!ext.ContainsKey("filename"))
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo(),
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "请求参数filename为空" },
                }
                           ));
            }
            string filename = ext["filename"];

            int offset = 0;

            if (ext.ContainsKey("offset"))
            {
                offset = int.Parse(ext["offset"]);
            }

            int size = 0;

            if (ext.ContainsKey("size"))
            {
                size = int.Parse(ext["size"]);
            }

            string filepath = this.genFilePathInternal(filename);

            if (!File.Exists(filepath))
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo {
                    StatusCode = HttpStatusCode.NotFound
                },
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "文件不存在" },
                }
                           ));
            }

            FileStream fs = new FileStream(Path.Combine(rootDir, filepath), FileMode.Open, FileAccess.Read);

            if (size == 0)
            {
                long realSize = fs.Length;
                if (realSize > 1024 * 1024 * 20)
                {
                    return(PackOrb.PackRespose(
                               new HttpHeadInfo {
                        StatusCode = HttpStatusCode.TooLarge
                    },
                               new ListDictionary {
                        { "respnum", -2 },
                        { "respmsg", "文件超长" },
                    }
                               ));
                }

                size = (int)realSize;
            }

            byte[] result  = new byte[size];
            int    realLen = fs.Read(result, offset, size);

            if (realLen < size)
            {
                Array.Resize(ref result, realLen);
            }

            string contentType = this.judgeFileType(filename);

            return(PackOrb.PackRespose(
                       new HttpHeadInfo
            {
                StatusCode = HttpStatusCode.Succeed,
                ContentType = judgeFileType(filename),
            },
                       result));
        }
Exemplo n.º 4
0
        private byte[] appendfile(string extInfo, byte[] req)
        {
            IDictionary <string, string> ext = DCUtil.ParaStrToDic(extInfo);

            if (!ext.ContainsKey("filename"))
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo(),
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "请求参数filename为空" },
                }
                           ));
            }
            string filename = ext["filename"];

            string filepath = this.genFilePathInternal(filename);

            try
            {
                FileStream fs = new FileStream(Path.Combine(rootDir, filepath), FileMode.OpenOrCreate, FileAccess.Write);
                if (ext.ContainsKey("offset"))
                {
                    long offset = Convert.ToInt64(ext["offset"]);
                    if (offset > fs.Length)
                    {
                        int gap = (int)(offset - fs.Length);
                        fs.Seek(0, SeekOrigin.End);
                        fs.Write(new byte[gap], 0, gap);
                    }
                    else if (offset < fs.Length)
                    {
                        fs.Seek(offset, SeekOrigin.Begin);
                    }
                }
                fs.Write(req, 0, req.Length);
                fs.Close();

                return(PackOrb.PackRespose(
                           new HttpHeadInfo {
                    StatusCode = HttpStatusCode.Succeed
                },
                           new ListDictionary {
                    { "respnum", req.Length },
                    { "respmsg", "文件追加成功" },
                    { "filename", filename },
                }
                           ));
            }
            catch (Exception ex)
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo {
                    StatusCode = HttpStatusCode.ServerSideError
                },
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "文件追加异常 " + ex.Message },
                }
                           ));
            }
        }