如何将HTML5 fileEntry文件发布到.net核心web api?

时间:2017-06-23 07:25:29

标签: html5 google-chrome-extension asp.net-core upload

我正在玩Chrome扩展程序。我的扩展程序有一个简单的弹出窗口,其中包含一个从Chrome FileSystem读取文件并将此文件POST到.net核心WebAPI的按钮。

function requestUpload() {
  window.requestFileSystem(window.PERSISTENT, 5 * 1024 * 1024,
    function (fs) {
      fs.root.getFile('ciccio.txt', {}, function (fileEntry) {

        // Get a File object representing the file,
        fileEntry.file(function (files) {

          let xhr = new XMLHttpRequest();
            xhr.open("post", "http://localhost:5000/api/Files/UploadFiles", true);
          xhr.setRequestHeader("Content-Type", "multipart/form-data");
          xhr.setRequestHeader("X-File-Name", files.name);
          xhr.setRequestHeader("X-File-Size", files.size);
          xhr.setRequestHeader("X-File-Type", files.type);
          xhr.send(files);
        }, errorHandler);

      }, errorHandler);
    }, errorHandler);
}

我的WebAPI操作如下:

[HttpPost("UploadFiles")]
public async Task<IActionResult> Post(IFormFile files)
{
    return Ok();
}

上传文件我获得了这个例外:

System.IO.InvalidDataException: Missing content-type boundary.

如何解决此问题?

由于

1 个答案:

答案 0 :(得分:0)

你的getFile()调用获取文件,返回一个文件对象。

public class UserAccountsListAdapter extends BaseAdapter {

private Context context;
private List<Users> accountList;
private BtnClickListener btnClickListener;
private SparseBooleanArray mSelectedItemsIds;

public UserAccountsListAdapter(Context context, List<Users> accountList, BtnClickListener btnClickListener) {
    this.context = context;
    this.accountList = accountList;
    this.btnClickListener = btnClickListener;
    mSelectedItemsIds = new SparseBooleanArray();
}

@Override
public int getCount() {
    return accountList.size();
}


@Override
public Object getItem(int position) {
    return accountList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.activity_account_layout2, null);
        convertView.setLongClickable(true);
    }

    Button button1 = (Button) convertView.findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            btnClickListener.onBtnClick();
        }
    });

    TextView accountEmail = (TextView) convertView.findViewById(R.id.user_acc_email);
    ProgressBar progressBar = (ProgressBar) convertView.findViewById(R.id.progress);
    progressBar.setMax(100);
    accountEmail.setText(accountList.get(position).getUserKey());

    return convertView;
}

public int getSelectedCount() {
return mSelectedItemsIds.size();
}

public void remove(UserAccountItem object) {
    accountList.remove(object);
    notifyDataSetChanged();
}

public void toggleSelection(int position) {
    selectView(position, !mSelectedItemsIds.get(position));
}
public void removeSelection() {
    mSelectedItemsIds = new SparseBooleanArray();
    notifyDataSetChanged();
}

public void selectView(int position, boolean value) {
    if (value)
        mSelectedItemsIds.put(position, value);
    else
        mSelectedItemsIds.delete(position);
    notifyDataSetChanged();
}


public SparseBooleanArray getSelectedIds() {
    return mSelectedItemsIds;
}
}

XMLHttpRequest.send()有以下重载:

fs.root.getFile('ciccio.txt', {}, function (fileEntry) {

File对象仅属于Blob类型,因此它也应该有效。但如果您未设置内容,它就会正确显示asp.net core may not workthisthis输入带有正确边界的标题。​​我认为使用formdata将正常工作。

请尝试以下操作,您无需设置多部分内容类型标题

void send();
void send(ArrayBuffer data);
void send(Blob data);
void send(Document data);
void send(DOMString? data);
void send(FormData data);