使用angularjs将带有表单表单数据的字节集合发送到api

时间:2015-07-24 13:47:53

标签: angularjs file api byte uploading

我必须将字节数组和字段数据一起发送到api,如图所示。我对使用angularjs社区提供的模块有疑问,因为它没有说明数据是如何传输的,我几乎没有了解它。 所以我想将从按钮上传的任何文件发布到带有表单数据的api的字节集合中。这是我的api的图像。

enter image description here

实际上我遇到的问题是将文件更改为角度字节并再次找到上传字节数组的模块。 我已经看到了一些字节上传技术,但与我需要的相比,它们会有所不同。如果你有更好的方法来看看这个,请告诉我一些

1 个答案:

答案 0 :(得分:1)

DAGM,

我没有足够的积分来为你的问题添加评论,所以我会在这里开始回答。

我在服务器上使用.NET Web API 2.0来接收和处理客户端传输。这是你的选择吗?

我确信除了Web API之外还有其他服务器选项,但我的答案的一半取决于此服务器端功能。

让我知道,我会尽力帮助你。

...问候

我至少可以给你一些与angular一起使用的客户端代码。在发送到服务器之前,我做了很多客户端压缩和压缩数据到字节数组。

请告诉我这是否有用。

这是一些客户端代码。

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?