Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Excel file client to server #3474


User avatar
0
michaelhilgers created

Hi,

I have a little problem. I uploaded a file (excel) in the clientsite (Angular 2).

But If I try to send it to the server I get an 500 error.

I modified the code in the service-proxies.ts like this:

createOrUpdatePostListe(file: any): Observable<void> {
        let url_ = this.baseUrl + "/api/services/app/Post/CreateOrUpdatePostListe";

        let fileTest: File = file.target.files[0];
        let formData: FormData = new FormData();
        formData.append('uploadFile', fileTest, fileTest.name);

        //const content_ = JSON.stringify(formData);
        const content_ = formData;
        
        return this.http.request(url_, {
            body: content_,
            method: "post",
            headers: new Headers({
                "Content-Type": "multipart/form-data", 
                "Accept": "application/json; charset=UTF-8"
            })
        }).map((response) => {
            return this.processCreateOrUpdatePostListe(response);
        }).catch((response: any, caught: any) => {
            if (response instanceof Response) {
                try {
                    return Observable.of(this.processCreateOrUpdatePostListe(response));
                } catch (e) {
                    return <Observable<void>><any>Observable.throw(e);
                }
            } else
                return <Observable<void>><any>Observable.throw(response);
        });
    }

I changed the Content-Type from "application/json" to "multipart/form-data".

If I use the "JSON.stringify(formData)" to set the content, my function on Server was called but the parameter with the excel sheet was empty.

How can I get my excel sheet on serversite ?

Thanks !


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    First of all, modifying service-proxies.ts is not a good idea, because when you add new app services and controllers you will need to refresh it and your changes will be gone.

    AspNet Zero contains an example of uploading a file. You can use same approach for your excel file. You can check ProfileControllerBase in Web.Core project and it's usage from angular app.

    Thanks.