我尝试从dxFileUploader(DevExpress)获取文件,但我无法在代码后面读取此文件。我只看到物体 我的FileUploader:
{
location: "before",
widget: "dxFileUploader",
options: {
multiple: false,
accept: "*",
value: [],
uploadMode: "instantly",
onValueChanged: (e) => {
$.ajax({
url: "api/CoordinateSystems/UploadFile",
type: "POST",
data: e.value,
error(xhr, textStatus, errorThrown) {
DxExtensions.notifyAjaxError(xhr, textStatus, errorThrown);
DxExtensions.error(errorThrown);
},
success(data) {
dataSource.load();
}
});
}
}
代码隐藏:
[HttpPost]
[Route("UploadFile")]
public IActionResult UploadFile(dynamic file)
{
List<string> errors = new List<string>(); // added this just to return something
if (file != null)
{
string physicalWebRootPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
// do something
}
return Ok();
}
如何获取文件,我可以保存在服务器上并使用它?
答案 0 :(得分:0)
这是AngularJS,但你可以使用它的一部分
HTML
<div dx-file-uploader="{
buttonText: 'Select file',
labelText: 'Drop file here',
multiple: false,
uploadMode: 'instantly',
bindingOptions: { uploadUrl: 'UploadUrl' ,disabled : 'IsReadOnly'},
showFileList: false,
onUploaded: on_Uploaded,
onInitialized: on_Initialized
}"></div>
的uploadURL
$scope.UploadUrl = CTHelper.ApiUrl() + '/Api/Case/PostFile';
API
[Route("Api/Case/PostFile")]
public async Task<IHttpActionResult> PostFile()
{
if (!Request.Content.IsMimeMultipartContent())
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
try
{
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
string path = System.Web.HttpContext.Current.Server.MapPath(Api.Helper.Constants.PortalFilesVirtualDir);
foreach (var file in provider.Contents)
{
var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
var buffer = await file.ReadAsByteArrayAsync();
File.WriteAllBytes(string.Format("{0}/{1}", path, filename), buffer);
//Do whatever you want with filename and its binaray data.
}
}
catch (Exception err)
{
HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.InternalServerError);
message.Content = new StringContent(err.Message);
throw new HttpResponseException(message);
}
return Ok();
}