Пример #1
0
        public static int SaveUploadFile(
            System.Web.UI.Page page,
            LibraryChannel channel,
            string strXmlRecPath,
            string strFileID,
            string strResTimeStamp,
            HttpPostedFile postedFile,
            int nLogoLimitW,
            int nLogoLimitH,
            out string strError)
        {
            strError = "";

            if (String.IsNullOrEmpty(postedFile.FileName) == true
                && postedFile.ContentLength == 0)
            {
                return 0;	// 没有必要保存
            }

            WebPageStop stop = new WebPageStop(page);

            string strResPath = strXmlRecPath + "/object/" + strFileID;

            string strLocalFileName = Path.GetTempFileName();
            try
            {
                using (Stream t = File.Create(strLocalFileName))
                {
                    // 缩小尺寸
                    int nRet = GraphicsUtil.ShrinkPic(postedFile.InputStream,
                            postedFile.ContentType,
                            nLogoLimitW,
                            nLogoLimitH,
                            true,
                            t,
                            out strError);
                    if (nRet == -1)
                        return -1;
                    if (nRet == 0)  // 没有必要缩放
                    {
                        postedFile.InputStream.Seek(0, SeekOrigin.Begin);
                        StreamUtil.DumpStream(postedFile.InputStream, t);
                    }
                }

                // t.Close();


                // 检测文件尺寸
                FileInfo fi = new FileInfo(strLocalFileName);

                if (fi.Exists == false)
                {
                    strError = "文件 '" + strLocalFileName + "' 不存在...";
                    return -1;
                }

                string[] ranges = null;

                if (fi.Length == 0)
                { // 空文件
                    ranges = new string[1];
                    ranges[0] = "";
                }
                else
                {
                    string strRange = "";
                    strRange = "0-" + Convert.ToString(fi.Length - 1);

                    // 按照100K作为一个chunk
                    ranges = RangeList.ChunkRange(strRange,
                        100 * 1024);
                }

                byte[] timestamp = ByteArray.GetTimeStampByteArray(strResTimeStamp);
                byte[] output_timestamp = null;

                // 2007/12/13 
                string strLastModifyTime = DateTime.UtcNow.ToString("u");

                string strLocalPath = postedFile.FileName;

                // page.Response.Write("<br/>正在保存" + strLocalPath);

            REDOWHOLESAVE:
                string strWarning = "";

                for (int j = 0; j < ranges.Length; j++)
                {
                REDOSINGLESAVE:

                    // Application.DoEvents();	// 出让界面控制权

                    if (stop.State != 0)
                    {
                        strError = "用户中断";
                        goto ERROR1;
                    }

                    string strWaiting = "";
                    if (j == ranges.Length - 1)
                        strWaiting = " 请耐心等待...";

                    string strPercent = "";
                    RangeList rl = new RangeList(ranges[j]);
                    if (rl.Count >= 1)
                    {
                        double ratio = (double)((RangeItem)rl[0]).lStart / (double)fi.Length;
                        strPercent = String.Format("{0,3:N}", ratio * (double)100) + "%";
                    }

                    if (stop != null)
                        stop.SetMessage("正在上载 " + ranges[j] + "/"
                            + Convert.ToString(fi.Length)
                            + " " + strPercent + " " + strLocalFileName + strWarning + strWaiting);

                    // page.Response.Write(".");	// 防止前端因等待过久而超时
                    long lRet = channel.SaveResObject(
stop,
strResPath,
                        strLocalFileName,
                        strLocalPath,
                        postedFile.ContentType, 
                        ranges[j],
j == ranges.Length - 1 ? true : false,	// 最尾一次操作,提醒底层注意设置特殊的WebService API超时时间
timestamp,
out output_timestamp,
out strError);

                    /*
                    long lRet = channel.DoSaveResObject(strResPath,
                        strLocalFileName,
                        strLocalPath,
                        postedFile.ContentType,
                        strLastModifyTime,
                        ranges[j],
                        j == ranges.Length - 1 ? true : false,	// 最尾一次操作,提醒底层注意设置特殊的WebService API超时时间
                        timestamp,
                        out output_timestamp,
                        out strError);
                     * */

                    timestamp = output_timestamp;

                    // DomUtil.SetAttr(node, "__timestamp",	ByteArray.GetHexTimeStampString(timestamp));

                    strWarning = "";

                    if (lRet == -1)
                    {
                        if (channel.ErrorCode == CirculationClient.localhost.ErrorCode.TimestampMismatch)
                        {

                            timestamp = new byte[output_timestamp.Length];
                            Array.Copy(output_timestamp, 0, timestamp, 0, output_timestamp.Length);
                            strWarning = " (时间戳不匹配, 自动重试)";
                            if (ranges.Length == 1 || j == 0)
                                goto REDOSINGLESAVE;
                            goto REDOWHOLESAVE;
                        }

                        goto ERROR1;
                    }


                }


                return 1;	// 已经保存
            ERROR1:
                return -1;
            }
            finally
            {
                // 不要忘记删除临时文件
                File.Delete(strLocalFileName);
            }
        }