samedi 5 avril 2014

c# - accéder à des fichiers téléchargés dans un gestionnaire générique - Stack Overflow


When I select a file and then submit I cant access the files that are supposed to be uploaded. There are a number of other statements in my handler function that didn't work either. I am using .NET 2.0, the error I am getting even though i have a file selected is the following:


"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"


I have the following form:


<form  runat="server" id = "selectFileForm" action="myDir/myHandler.ashx/fileUpload" method="POST"         enctype="multipart/form-data" />
<input runat="server" type = 'file' id = 'fileUpload' multiple ='multiple'>
<input type="hidden" id = "cmd" name = "cmd" value=9>
</form>

I tried this with runat=server just on the form element and that didn't work. SO then I tried it also on the input element, neither way worked.


Here is what I have on the back end in my ashx:


private string fileUpload(HttpContext context)
{
//foreach (string f in context.Request.Files.AllKeys)
//{
// context.Request.Files[f].
//}

//HttpPostedFile fileupload = context.Request.Files[0];

//return "{\"success\" : \"" + fileupload.FileName +"\"}";

//return "{\"success\" : \"" + context.Request.Files["fileUpload"] +"\"}";

return "{\"success\" : \"" + context.Request.Files[0] +"\"}";
}


When I select a file and then submit I cant access the files that are supposed to be uploaded. There are a number of other statements in my handler function that didn't work either. I am using .NET 2.0, the error I am getting even though i have a file selected is the following:


"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"


I have the following form:


<form  runat="server" id = "selectFileForm" action="myDir/myHandler.ashx/fileUpload" method="POST"         enctype="multipart/form-data" />
<input runat="server" type = 'file' id = 'fileUpload' multiple ='multiple'>
<input type="hidden" id = "cmd" name = "cmd" value=9>
</form>

I tried this with runat=server just on the form element and that didn't work. SO then I tried it also on the input element, neither way worked.


Here is what I have on the back end in my ashx:


private string fileUpload(HttpContext context)
{
//foreach (string f in context.Request.Files.AllKeys)
//{
// context.Request.Files[f].
//}

//HttpPostedFile fileupload = context.Request.Files[0];

//return "{\"success\" : \"" + fileupload.FileName +"\"}";

//return "{\"success\" : \"" + context.Request.Files["fileUpload"] +"\"}";

return "{\"success\" : \"" + context.Request.Files[0] +"\"}";
}

0 commentaires:

Enregistrer un commentaire