Sending byte collections with form form data to api using angularjs

泄露秘密 提交于 2019-12-25 07:58:12

问题


I have to send a byte array to an api along with the field data as shown in the figure.I have a doubt about using the modules provided by angularjs community because its not stating about how the data is transferred and I have little understanding about it. so I want to post any file uploaded from a button to a byte collection to an api with the form data. here is an image for my api.

Actually the problem I faced is changing the file to byte in angular and again finding a module to upload the byte array. I have seen some of byte uploading techniques but they are quit different compared to what I need.If you have some better ways to look at this please show me some


回答1:


Dagm,

I do not have enough points to add a comment to your question, so I will start an answer here.

I am using the .NET Web API 2.0 on the server to receive and process the client transmission. Is this an option for you?

I am sure that there are other server options other than Web API, but half of my answer depends on this server-side functionality.

Let me know, and I will try to assist you.

Regards....

I can at least give you some client code that works with angular. I do a lot of client side compaction and compression of data into byte arrays before sending to the server.

Let me know if this would be useful.

Here is some client code.

ItemHttpService.prototype.uploadItem = function ($http, Id, Transaction_Id, FileName, FileExt, File, itemUploadCallback) {
        $http({
            // Web API 2.0 specific. ItemUpload references the ItemUploadController. UploadItems is method name.
            url: 'api/ItemUpload/UploadItems',
            method: 'POST',
            headers: { 'Content-Type': 'application/octet-stream' },
            data: new Uint8Array(File),
            params: {
                // Other params here, including string metadata about uploads
                Id: Id,
                Transaction_Id: Transaction_Id,
                FileName: FileName,
                FileExt: FileExt
            },
            transformRequest: [] // Used to handle the byte array data.
        }).success(function (response, status) {// This is one example of how I handled a response.
            if (status === 200) {
                itemUploadCallback(response); // As I recall, the response is a JSON stringified result. 
            } else {
                itemUploadCallback('');
            }
        }).error(function (status) {
            itemUploadCallback(JSON.stringify("Your error handling here"));
        });
    };

Not sure what the tblPeriodical_Transaction is. Can you elaborate?


来源:https://stackoverflow.com/questions/31612259/sending-byte-collections-with-form-form-data-to-api-using-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!