使用 Dojo 上传文件,使用 xhr Post 上传 c#

本文关键字:使用 Post 上传 xhr 文件 Dojo | 更新日期: 2023-09-27 17:56:44

我正在尝试使用 dojo/_base/xhr 上传文件这是代码:

 xhr.post({
                url: postUrl,
                handleAs: "text",
                contentType: "application/json",
                timeout: 10000,
                postData: dojo.toJson({ file: Uploader.files[0]}),
                load: function (result) {
                    show image...
                },
                error: function (err) {
                    show error...
                }
            });

当我尝试发送Uploader.files[0].size时,我得到我应该得到的值,但是当我尝试发送Uploader.files[0]Uploader.files[0]时,我得到 null。

在服务器端:

[HttpPost]
public string UploadImg(string file)
{
`   Saving file
}

我什么都试过了!!但是我无法获取文件本身。 Request.Files返回 0 个文件。提交表单不是一种选择,当我使用

 xhr.post({
                form: dom.byId("myform"),
                handleAs: "text",
                timeout: 10000,
                load: function (result) {
                    show image...
                },
                error: function (err) {
                    show error...
                }

Request.Files返回 0

使用 Dojo 上传文件,使用 xhr Post 上传 c#

ajax 不是跨浏览器文件异步上传的可行选择。 您应该尝试使用以下模块:

http://dojotoolkit.org/reference-guide/1.8/dojo/request/iframe.html

http://dojotoolkit.org/reference-guide/1.9/dojo/request/iframe.html

并让他们将您的表单提交到隐藏的 iframe。

iframe(postUrl,{
  form: "theForm",
  handleAs: "text"
}).then(function(data){
  show image...
},function(err){
  show error...
});

请记住,如果您需要对返回的数据执行某些操作(并且不是 HTML [如您的文本大小写]),您需要为响应执行此操作:

<html>
  <body>
    <textarea>
         data
    </textarea>
  </body>
</html>

您可以尝试使用 HttpPostedFileBase :

[HttpPost]
public string UploadImg(HttpPostedFileBase file) {
   //Save the file :
   if (file != null && file.ContentLength > 0) {
      file.SaveAs(path);
   }
}

请参阅此 StackOverflow 答案和本文

您也可以尝试删除 dojo.toJson :

postData: { file: Uploader.files[0]}

而不是:

postData: dojo.toJson({ file: Uploader.files[0]})