简体   繁体   中英

Receive file with servicestack from multipart/form-data

I'm submitting a form as a multipart/form-data from a react app to .net backend. I use FormData on react to post data using axios. On the serverside I'm using servicestack to process the data, text inputs I managed to figure out but the file input I don't know how to receive.

This is how I submit my form from react

handleSubmit = event => {
        event.preventDefault();
        var form = document.getElementById("imageForm"); // You need to use standard javascript object here
        var formData = new FormData(form);

        axios.post('http://myurls.com', formData, {headers: { 'Content-Type': 'multipart/form-data' }})
        .then(res=>{
            console.log(res);
        }) 
        .catch(error =>{
            console.log(error);
        })
    }

This is my servicestack C#


    #region PLUGIN
    public class FormSubmit : IPlugin
    {
        public void Register(IAppHost appHost)
        {
            appHost.RegisterService(typeof(FormSubmitService));
        }
    }

    #endregion

    #region SERVICE
    public class FormSubmitService : ServiceStack.Service
    {
        public object Post(FormSubmitRequest request)
        {
            return request;
        }
    }
    #endregion

    #region REQUEST
    [Route("/myservice/submitform")]
    public class FormSubmitRequest : IReturn<object>
    {
        public object ImageFile { get; set; }
        public string ImageTitle { get; set; }
        public string ImageDescription { get; set; }

    }
    #endregion

I'm very new to C# and I can't pinpoint what the issue is. My guess was the datatype of the ImageFile, I tried setting it to object , string , bytes[] and System.Web.UI.WebControls.Image but that didn't seem to make any difference.

When I'm returning the request I can see my request object which contains ImageTitle and ImageDescription but no ImageFile , not even an empty key

I'm really lost and don't even know where to begin debugging the issue.

Thanks in advance for any help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM