4

コントローラ:

private readonly Dictionary<string, Stream> streams;

        public ActionResult Upload(string qqfile, string id)
        {
            string filename;
            try
            {
                Stream stream = this.Request.InputStream;
                if (this.Request.Files.Count > 0)
                {
                    // IE
                    HttpPostedFileBase postedFile = this.Request.Files[0];
                    stream = postedFile.InputStream;
                }
                else
                {
                    stream = this.Request.InputStream;
                }

                filename = this.packageRepository.AddStream(stream, qqfile);
            }
            catch (Exception ex)
            {
                return this.Json(new { success = false, message = ex.Message }, "text/html");
            }

            return this.Json(new { success = true, qqfile, filename }, "text/html");
        }

ストリームを追加する方法:

        public string AddStream(Stream stream, string filename)
        {

            if (string.IsNullOrEmpty(filename))
            {
                return null;
            }

            string fileExt = Path.GetExtension(filename).ToLower();
            string fileName = Guid.NewGuid().ToString();
            this.streams.Add(fileName, stream);
        }

私は次のようにバイナリストリームを読み込もうとしています:

Stream stream;
            if (!this.streams.TryGetValue(key, out stream))
            {
                return false;
            }

    private const int BufferSize = 2097152;

                            using (var binaryReader = new BinaryReader(stream))
                            {
                                int offset = 0;
                                binaryReader.BaseStream.Position = 0;
                                byte[] fileBuffer = binaryReader.ReadBytes(BufferSize); // THIS IS THE LINE THAT FAILS
    ....

ストリームをデバッグ モードで表示すると、read = true、seek = true、length = 903234 などになることが示されます。

しかし、私は取得し続けます: 閉じたファイルにアクセスできません

これは、mvc サイトをローカル/デバッグ モード (VS IIS) で実行すると正常に動作し、「リリース」モード (サイトが iis に公開されたとき) では動作しません。

私は何を間違っていますか?

4

2 に答える 2

8

Found solution here:

uploading file exception

Solution:

add "requestLengthDiskThreshold" on production envirenment

<system.web>
<httpRuntime executionTimeout="90" maxRequestLength="20000" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="8192"/>
</system.web>
于 2013-02-15T08:52:56.147 に答える
0

制御していないオブジェクト (HttpRequest オブジェクトのプロパティ) の有効期間に依存しているように見えます。ストリームのデータを保存したい場合は、そのデータをすぐにバイト配列などにコピーする方が安全です

AddStream をに変更できます

    public string AddStream(Stream stream, string filename)
    {

        if (string.IsNullOrEmpty(filename))
        {
            return null;
        }

        string fileExt = Path.GetExtension(filename).ToLower();
        string fileName = Guid.NewGuid().ToString();
        var strLen = Convert.ToInt32(stream.Length);
        var strArr = new byte[strLen];
        stream.Read(strArr, 0, strLen);
        //you will need to change the type of streams acccordingly
        this.streams.Add(filename,strArr); 
    }

ストリームのデータが必要な場合は、配列を使用して、データが格納されているオブジェクトの有効期間を完全に制御できます。

于 2013-02-12T12:47:26.540 に答える