javascript – 使用Aurelia将数据和文件发布到ASP.NET webapi
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 使用Aurelia将数据和文件发布到ASP.NET webapi,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2794字,纯文字阅读大概需要4分钟。
内容图文
我正在尝试将带有文件上传的输入添加到我的应用程序中.
这是我的视图,有两个输入,一个文本和一个文件:
<template>
<form class="form-horizontal" submit.delegate="doImport()">
<div class="form-group">
<label for="inputLangName" class="col-sm-2 control-label">Language key</label>
<div class="col-sm-10">
<input type="text" value.bind="languageKey" class="form-control" id="inputLangName" placeholder="Language key">
</div>
</div>
<div class="form-group">
<label for="inputFile" class="col-sm-2 control-label">Upload file</label>
<div class="col-sm-10">
<input type="file" class="form-control" id="inputFile" accept=".xlsx" files.bind="files">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Do import</button>
</div>
</div>
</form>
</template>
在我的webapi中,我有这个代码,我从here复制并粘贴:
public class ImportLanguageController : ApiController
{
public async Task<HttpResponseMessage> Post()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
//Trace.WriteLine(file.Headers.ContentDisposition.FileName);
//Trace.WriteLine("Server file path: " + file.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
}
最后我在Aurelia有我的视图模型:
import {inject} from 'aurelia-framework';
import {HttpClient, json} from 'aurelia-fetch-client';
@inject(HttpClient)
export class Import {
languageKey = null;
files = null;
constructor(http){
http.configure(config => {
config
.useStandardConfiguration();
});
this.http = http;
}
doImport() {
//What goes here??
}
}
所以我的问题是,我的函数doImport中有什么逻辑?我不确定webapi中我的控制器方法中的代码是否正确,随便对此发表评论.
解决方法:
这应该可以帮助您入门:
doImport() {
var form = new FormData()
form.append('language', this.languageKey)
form.append('file', this.files)
//Edit, try this if the first line dont work for you
//form.append('file', this.files[0])
this.http.fetch('YOUR_URL', {
method: 'post',
body: form
})
.then( response => {
// do whatever here
});
}
根据您提供的webapi响应(如果有),您可能希望使用以下内容:
.then( response => response.json() )
.then( response => {
// do whatever here
});
我还应该提到的一件事是fetch使用COR,所以如果你遇到任何CORS错误,你可能需要在服务器端启用它们.
这是Aurelia部分的gist.run(除非您更改URL,否则发布将无效):
https://gist.run/?id=6aa96b19bb75f727271fb061a260f945
内容总结
以上是互联网集市为您收集整理的javascript – 使用Aurelia将数据和文件发布到ASP.NET webapi全部内容,希望文章能够帮你解决javascript – 使用Aurelia将数据和文件发布到ASP.NET webapi所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。