2011-08-06 4 views
2

私はヴァルムのAJAXはスクリプトをアップロードしてプレーしてきた:http://valums.com/ajax-upload/バリュームAjax Upload with C#ashxは 'インデックスが範囲外です。負でなく、コレクションのサイズより小さくなければなりません。エラー

それは、いずれかの恐ろしいSWFUploadのフラッシュオブジェクトを使用していない、私のためにすべてのボックスを刻みます。私は私の.ashxスクリプト(私はデバッグすることができない、お金のために)のJSポイントを持っています。これは私が.ashxに持っているものです。

パブリッククラスアバター:IHTTPハンドラ、System.Web.SessionState.IRequiresSessionState {

public void ProcessRequest (HttpContext context) { 
    //Debugger.Break(); 

string result = "{\"success\":true}"; 
    string path = HttpContext.Current.Server.MapPath("/client/vault/" + Helpers.CurrentClientHash(context.Session["SessionHash"].ToString()) + "https://stackoverflow.com/users/" + context.Session["SessionHash"].ToString() + "/"); 
    string saveLocation = string.Empty; 
    string fileName = string.Empty; 

    try 
    { 
     int length = 4096; 
     int bytesRead = 0; 
     Byte[] buffer = new Byte[length]; 

     //This works with Chrome/FF/Safari 
     // get the name from qqfile url parameter here 
     Debugger.Break(); 
     fileName = context.Request["params"]; 
     Debug.Write(fileName); 

     saveLocation = context.Server.MapPath(path) + fileName; 

     try 
     { 
      using (FileStream fileStream = new FileStream(saveLocation, FileMode.Create)) 
      { 
       do 
       { 
        bytesRead = context.Request.InputStream.Read(buffer, 0, length); 
        fileStream.Write(buffer, 0, bytesRead); 
       } 
       while (bytesRead > 0); 
      } 
     } 
     catch (UnauthorizedAccessException ex) 
     { 
      // log error hinting to set the write permission of ASPNET or the identity accessing the code 
      result = result.Replace("true","false, \"error\":" + ex.Message + " " + ex.InnerException + " " + ex.StackTrace.ToString()); 
     } 
} 
catch 
{ 
    try 
     { 
      //This works with IE 
      fileName = Path.GetFileName(context.Request.Files[0].FileName); 
      saveLocation = context.Server.MapPath(path) + fileName; 
      context.Request.Files[0].SaveAs(saveLocation); 
     } 
    catch (Exception ex) 
     { 
      result = result.Replace("true", "false, \"error\":" + ex.Message + " " + ex.InnerException); 
     } 
} 
context.Response.Write(result); 
} 

public bool IsReusable { 
    get { 
     return false; 
    } 
} 

}

このコードは親切に別のユーザーによってささげましたそれはPHPのサーバー側のものと一緒に出荷されるため、Valumのスクリプトです。私は、アップローダを実行すると、私はコンソールでこれを取得する:

[アップローダー] responseText = {「成功」:falseの場合、「エラー」:インデックスが 範囲外でした。負でなく、コレクションのサイズより小さくなければなりません。 パラメータ名:index}

...もちろんアップロードは失敗します。私はそれがFileStreamと何か関係があると確信していますが、意味のあるデバッグがなければ私は問題を見つけることができません。私はそれが.ashxで、ファイルがされていないので、ピックアップされたかもしれないと思うが、それはのparamsにあります:

bust uploader...

だから、私は2つの質問私は可能性がある場合があります。

  1. 誰でも見ることができますか、どこで、なぜ私はインデックスの例外を得ていますか?
  2. もしそうでなければ、どうすればこの問題をデバッグできますか?私はJSの負荷がかかるようではないので、VS2010からデバッガを実行することはできません。私は明らかにashxに直接行くことはできません...いずれかのアイデア?

ヘルプは大歓迎:)

答えて

4

、私はこれを解決することはありません。私はValamsスクリプトをアンインストールしてPluploadに行った。

Pluploadは簡単で、HTML5、Flash、Gears、BrowserPlusをサポートしています。それは最後には考えがなかった。ここではC#AJAXアップローダーウィジェットをお探しの方のための作業コードは次のとおりです。

<script type="text/javascript" src="/js/jquery.min.js"></script> 

    <script type="text/javascript" src="http://bp.yahooapis.com/2.4.21/browserplus-min.js"></script> 
    <script type="text/javascript" src="/js/plupload.js"></script> 

    <script type="text/javascript" src="/js/plupload.html5.js"></script> 
    <script type="text/javascript" src="/js/plupload.gears.js"></script> 
    <script type="text/javascript" src="/js/plupload.browserplus.js"></script> 
    <script type="text/javascript" src="/js/plupload.silverlight.js"></script> 
    <script type="text/javascript"> 
     // Custom example logic 
     function $(id) { 
      return document.getElementById(id); 
     } 


     var uploader = new plupload.Uploader({ 
      runtimes: 'gears,html5,silverlight,browserplus', 
      browse_button: 'pickfiles', 
      max_file_size: '2mb', 
      multi_selection: false, 
      url: '/components/uploadify/avatar.ashx', 
      silverlight_xap_url: '/js/plupload.silverlight.xap', 
      filters: [ 
       { title: "Image files", extensions: "jpg,gif,png" } 
      ] 
     }); 

     uploader.bind('Init', function (up, params) { 
      $('filelist').innerHTML = "<div>Current runtime: " + params.runtime + "</div>"; 
     }); 

     uploader.bind('FilesAdded', function (up, files) { 
      for (var i in files) { 
       $('filelist').innerHTML += '<div id="' + files[i].id + '">' + files[i].name + ' (' + plupload.formatSize(files[i].size) + ') <b></b></div>'; 
      } 
     }); 

     uploader.bind('UploadFile', function (up, file) { 
      $('uploader').innerHTML += '<input type="hidden" name="file-' + file.id + '" value="' + file.name + '" />'; 
     }); 

     uploader.bind('UploadProgress', function (up, file) { 
      $(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>"; 
     }); 

     uploader.bind('FileUploaded', function (up, file, obj) { 
      alert("I've done uploading stuff..."); 

     }); 

     $('uploadfiles').onclick = function() { 
      uploader.start(); 
      return false; 
     }; 

     uploader.init(); 
</script> 

とC#の.ashx ...

public class avatar : IHttpHandler, System.Web.SessionState.IRequiresSessionState { 

    public void ProcessRequest (HttpContext context) { 
     string path = "https://stackoverflow.com/a/path/to/someplace/"; 
     if (context.Request.Files.Count > 0) 
     { 
      int chunk = context.Request["chunk"] != null ? int.Parse(context.Request["chunk"]) : 0; 
      string fileName = context.Request["name"] != null ? context.Request["name"] : string.Empty; 

      HttpPostedFile fileUpload = context.Request.Files[0]; 

      var uploadPath = path; 
      using (var fs = new FileStream(Path.Combine(uploadPath, fileName), chunk == 0 ? FileMode.Create : FileMode.Append)) 
      { 
       var buffer = new byte[fileUpload.InputStream.Length]; 
       fileUpload.InputStream.Read(buffer, 0, buffer.Length); 

       fs.Write(buffer, 0, buffer.Length); 
      } 
     } 

    } 

    public bool IsReusable { 
     get { 
      return false; 
     } 
    } 

} 

それは多分価値のあるあなたがセッションにアクセスしたい場合ということ、を指摘します.ashxでは、図のようにSessionState.IRequiresSessionStateを追加するだけです。

私が覗いては、これが役に立つことを願って:)

+0

+1それはあなた自身で解決します。 –

1

私は、これらの結果を生成する参照唯一の例外ハンドラは、「これはIEで動作します」ブロックです。このブロック内で参照される唯一のインデックスはFiles [0]です。

最初のtry/catchにキャッチ(Exception ex)を追加して、Safariで動作するコードがIEで動作しない理由を判断することをお勧めします。もう1つの注意点は、すべてのブラウザのストリームを読み込んでから、IE用にストリームを再読み込みしようとしていることです。これは、このサーバー側のスクリプトは、すべてのブラウザで動作するかどうかを確認してください位置0

にストリームをリセットする必要になります。

残念ながら
using System; 
using System.Web; 
using System.IO; 
public class Upload : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     string path = HttpContext.Current.Server.MapPath("/client/vault/" 
      + Helpers.CurrentClientHash(context.Session["SessionHash"].ToString()) 
      + "https://stackoverflow.com/users/" + context.Session["SessionHash"].ToString() 
      + "/"); 

     HttpPostedFile oFile = 
        context.Request.Files[context.Request.Headers["X-File-Name"]]; 

     if (!Directory.Exists(path)) Directory.CreateDirectory(path);  
     oFile.SaveAs(path + oFile.FileName);  

     context.Response.Write("1"); 
    } 
    public bool IsReusable 
    {  
     get { return true; } 
    } 
} 
+0

おかげで、コードは、あなたが提供したsniipet Uploadifyのためのバックエンド・スクリプトで、Valamsスクリプトは動作しません。しかし私はtry/catchを試して、それがどこにあるのか見てみましょう! – dooburt

+0

+1を助けてクリスを助けてください:) – dooburt

3

私はサイードバッシャーが彼のblogで提供ASHXハンドラを使用。それは完全に動作します。答えを

public void ProcessRequest(HttpContext context) 
    { 
     const string path = "Capture/Images"; 
     String filename = HttpContext.Current.Request.Headers["X-File-Name"]; 
     if (string.IsNullOrEmpty(filename) && HttpContext.Current.Request.Files.Count <= 0) 
     { 
      context.Response.Write("{success:false}"); 
     } 
     else 
     { 
      string mapPath = HttpContext.Current.Server.MapPath(path); 
      if (Directory.Exists(mapPath) == false) 
      { 
       Directory.CreateDirectory(mapPath); 
      } 
      if (filename == null) 
      { 
       //This work for IE 
       try 
       { 
        HttpPostedFile uploadedfile = context.Request.Files[0]; 
        filename = uploadedfile.FileName; 
        uploadedfile.SaveAs(mapPath + "\\" + filename); 
        context.Response.Write("{success:true, name:\"" + filename + "\", path:\"" + path + "/" + filename + "\"}"); 
       } 
       catch (Exception) 
       { 
        context.Response.Write("{success:false}"); 
       } 
      } 
      else 
      { 
       //This work for Firefox and Chrome. 
       FileStream fileStream = new FileStream(mapPath + "\\" + filename, FileMode.OpenOrCreate); 
       try 
       { 
        Stream inputStream = HttpContext.Current.Request.InputStream; 
        inputStream.CopyTo(fileStream); 
        context.Response.Write("{success:true, name:\"" + filename + "\", path:\"" + path + "/" + filename + "\"}"); 
       } 
       catch (Exception) 
       { 
        context.Response.Write("{success:false}"); 
       } 
       finally 
       { 
        fileStream.Close(); 
       } 
      } 
     } 
    } 
+0

これは実際にはかなり良く見えます。私はこれを試してみるかもしれない。 +1;) – dooburt

関連する問題

 関連する問題