再会,
我正在尝试将 javascript blob 图像发送到 ASP.NET Core 中的控制器方法,但它不会发送到我的控制器。
我的画布上有一张图像,它dataUri能够将其转换为 javascript blob,我正在使用以下代码:
dataURItoBlob(dataURI) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
var byteString = atob(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
// create a view into the buffer
var ia = …Run Code Online (Sandbox Code Playgroud) 今天是个好日子,
我正在尝试使用ajax从客户端上传文件到服务器端(asp.net核心)控制器,但我有一个空值.
这是我的html和javascript代码:
<input type="file" id="myfile" class="required" />
<button type="button" class="btn btn-info" onclick="uploadcsvfile()">
<script>
function uploadcsvfile() {
var myfile= document.getElementById("myfile");
var formData = new FormData();
if (myfile.files.length > 0) {
for (var i = 0; i < myfile.files.length; i++) {
formData.append('file-' + i, myfile.files[i]);
}
}
$.ajax({
url: "/MyController/UploadFile/",
type: "POST",
dataType: "json",
data: formData,
contentType: false,
processData: false,
success: function(data){
},
error: function (data) {
}
})
}
</script>
Run Code Online (Sandbox Code Playgroud)
这是我使用IFormFile的控制器
public async Task<JsonResult> UploadFile(IFormFile formData)
{
// do something …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用vue.js和axios上传多个文件/图像。我的后端是ASP.NET Core。但是问题是,每当我在请求方法中放置断点时,我的控制器中总会得到一个Count = 0。
这是我的简单HTML和代码:
的HTML
<div id="app">
<form enctype="multipart/form-data">
<input type="file" name="file" multiple="" v-on:change="fileChange($event.target.files)"/>
<button type="button" @@click="upload()">Upload</button>
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
我的JS
import axios from "axios"
var app= new Vue({
el: "#app",
data: {
files: new FormData()
},
methods:{
fileChange(fileList) {
this.files.append("file", fileList[0], fileList[0].name);
},
upload() {
const files = this.files;
axios.post(`/MyController/Upload`, files,
{
headers: {
'Content-Type': 'multipart/form-data'
}
}).then(response => {
alert(response.data);
}).catch(error => {
console.log(error);
});
},
}
Run Code Online (Sandbox Code Playgroud)
我的控制器
public IActionResult Upload(IList<IFormFile> files)
{
return Json("Hey");
}
Run Code Online (Sandbox Code Playgroud)
有什么帮助吗?
再会,
我在我的身份验证中使用 jwt。我已经解码了我的令牌,但问题是,我想检查令牌exp是否已经过期。
var decodedToken = localStorage.getItem('user_token');
console.log(decodedToken.exp) // writes 1540360205
Run Code Online (Sandbox Code Playgroud)
先感谢您。
我有一个获取表格的代码。这是我的示例代码:
public IQueryable<MyItem> MyItems(){
return context.MyItem;
}
Run Code Online (Sandbox Code Playgroud)
这是 MyItem 的示例属性
public int Id {get;set;}
public byte[] Image {get;set;}
public string Name {get;set;}
Run Code Online (Sandbox Code Playgroud)
因为,byte[]可以有多个字符,所以我不想在搜索中包含 then,因为如果我有像 10,000 个项目这样的记录需要很长时间。
通常,我希望Select这样:
public IQueryable<MyItem> MyItems(){
return context.MyItem.Select(item=>new MyItem{
Id=item.Id,
Name=item.Name
});
}
Run Code Online (Sandbox Code Playgroud)
这对于少数属性来说是可以的,但是我有 10-20 个属性,一个一个地写它们会很麻烦。
有没有什么办法,我只是Image为了更短的代码而在 lambda 中的属性除外?
再会,
我正在使用 axios.get 遇到此错误代码。错误代码是 415。我想要做的是,提供 const 对象将其发送到我的控制器。我试图调试控制器,但它没有在我的控制器(ASP.NET Core 控制器)中进行。
这是我的示例代码:
// Class that I want to supply
public class User{
public int? Name {get;set;}
public DateTime? Bday {get;set;}
}
// My Controller
public async Task<IActionResult> GetUsers([FromBody] User user){
// Do something here
}
// My js file axios is already imported and working
async searchUser(){
const user = {
Name: name,
Bday: bday
}
await axios.get(`/SomePage/GetUsers/`,user).then(response=>{
// do something here
}.catch(error=>{console.log(error);});
}
Run Code Online (Sandbox Code Playgroud)
我希望有人能帮我找到解决方案。
这是示例场景,我有3种不同的方法,不同的参数,但结果是属性类的返回列表相同.
这是我的代码:
// My Model class:
public class Users{
public string Name{ get; set; }
public int Age { get; set; }
public string Location { get; set; }
}
// My Vm class:
public class UserVm {
public string Name{ get; set; }
public int Age { get; set; }
public string Location { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的lambda查询
// by name
public IEnumerable<UserVm> GetUsersByName (string name){
return db.Users.Where(x=>x.Name == name).Select(users=>new UserVm{
Name = users.Name,
Age = users.Age,
Location …Run Code Online (Sandbox Code Playgroud) 今天是个好日子,
我需要一个关于如何提交一个包含对象数组的对象的帮助,对象数组应该来自我制作的复选框.
这是我的示例复选框
<input type="checkbox" name="user.preferences" value="Hard" />
<input type="checkbox" name="user.preferences" value="Soft" />
<input type="checkbox" name="user.preferences" value="Small" />
Run Code Online (Sandbox Code Playgroud)
我的数据javascript看起来像这样:
user:{
preferences: []
}
Run Code Online (Sandbox Code Playgroud)
当我提醒user使用时JSON.stringify,我能够看到类似这样的结果.
{"preferences": ["Soft","Small"]}
Run Code Online (Sandbox Code Playgroud)
但问题是,我使用的api需要这样的格式:
{
"preferences": [
{
"preference": "Hard"
},
{
"preference": "Soft"
},
// so on and so forth
]
}
Run Code Online (Sandbox Code Playgroud)
请有人帮帮我 谢谢
再会,
我正在尝试使用 HTML 表格创建动态日历。我现在面临一些问题。
我不知道如何指定该月的特定第一天是星期日、星期一、星期二、星期三、星期四还是星期六。
如何修复我的表格主体以像日历一样正确显示表格。
到目前为止,这些是我在剃刀页面中的代码。
@{ // responsible for getting the first and last days of the month
var getDate = DateTime.Now;
var firstDayOfTheMonth = new DateTime(getDate.Year, getDate.Month, 1);
var lastDayOfTheMonth = firstDayOfTheMonth.AddMonths(1).AddDays(-1);
var numberOfDays = Convert.ToInt16(lastDayOfTheMonth.ToString("dd"));
}
// My HTML table
<table border="1">
<thead>
<tr>
<th>Sunday</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
</tr>
</thead>
<tbody>
<tr>
@for (var i = 0; i < numberOfDays + 1; i++)
{
<td>@i</td>
}
</tr>
</tbody>
Run Code Online (Sandbox Code Playgroud)
先感谢您。
c# ×5
javascript ×5
axios ×4
asp.net ×2
json ×2
lambda ×2
linq ×2
ajax ×1
arrays ×1
asp.net-core ×1
asp.net-mvc ×1
date ×1
file-upload ×1
jquery ×1
jwt ×1
object ×1
razor ×1
vue.js ×1