Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am trying to upload files in one of the network path. but I couldn't able to upload files using HttpContext.Current.Request.Files.Count.

My problem is Count always coming less than zero.

Note: am using React Js in .net

From React js am calling Generic handler.

Please find code below,

React Js Code:
JavaScript
const formdata=new FormData();
formdata.append('myfile',file[0]);

fetch('Render/Uploadhandler.ashx',{
    Method: 'POST',
    body: formdata,
})
.then(function(res)){
    if(res.ok)
    {
        alert('success');
    }
    else
    {
        alert('failure');
    }
})

Generic hander Code:
C#
public void ProcessRequest(HttpContext context)
{
    if (context.Current.Request.Files.Count > 0)
    {
        // TO DO HERE
    }
    
}

Can someone please Suggest me how to proceed this.

Thanks in advance.

What I have tried:

C#
public void ProcessRequest(HttpContext context)
{
    if (context.Current.Request.Files.Count > 0)
    {
        // TO DO HERE
    }
    
}
Posted
Updated 1-Jul-21 4:13am
v2
Comments
Richard Deeming 10-Nov-20 6:48am    
Why are you using context.Current.Request instead of simply context.Request?
Richard Deeming 10-Nov-20 6:49am    
Assuming the files array is correct, your Javascript code looks OK according to the documentation:
Using Fetch - Web APIs | MDN[^]

Try using your browser's developer tools to inspect the network request.
GuyThiebaut 10-Nov-20 12:54pm    
That fetch looks odd to me.
You need to provide the endpoint in the fetch, not the file name that handles the endpoint.
Your Uploadhandler.ashx file should expose a POST endpoint which you can post to, it is that endpoint that needs to be in the fetch.

Put a debug point on your endpoint and see if it gets hit when the post is issued.
I think you also need to get rid of the last closing bracket in (function(res))
stellus 12-Nov-20 7:59am    
Thanks For the reply,
I was checked Network by using Developer tools,
I got the below Header response for formdata.
-- Content-Disposition : form-data; name="myfile"
[Object Object]
-- Webkitformboundary....

1 solution

Most probably this might be the reason:

Use
if you are using only

enctype="multipart/form-data" allows Files data to get populated in this
HttpContext.Request.Form.Files

Also make sure you have a name attribute associated with your input file type

like this
 
Share this answer
 
Comments
Richard Deeming 1-Jul-21 11:16am    
If you read the question, you will see that the OP is using the Fetch API[^] to submit the form. The enctype on the form and the name on the input are irrelevant.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900