Dojo文件上传C#MVC.NET HttpPost没有Uploader

时间:2017-08-31 13:21:55

标签: c# asp.net-mvc dojo http-post

我尝试使用HTML / JavaScript / C#代码找到一个很好的示例,以提供有关仅将一个文件上载到服务器以将其保存到服务器上的目录的说明。我的代码在调试时将我的变量' file'给了我。

HTML表单:

<form id="uploadForm" method="post" data-dojo-type="dijit/form/Form" enctype="multipart/form-data">
    <input
        id="fileUploadInput"
        type="file"
        name="fileUploadInput"
    >
    <br />
    <br />
    <button
        id="fileUploadButton"
        data-dojo-attach-point="fileUploadButton"
        onClick="click"
    >
    Upload
    </button>
</form>

Dojo / JavaScript代码:

ioIframe.send({
    url: this.proxyPostFile,
    form: "uploadForm",
    method: "POST",
    handleAs: "text",
    // Callback on successful data call:
    load: function (response, ioArgs) {
        return response;
    },
    // Callback on errors
    error: function (response, ioArgs) {
        console.info(response)
    }
});

c#c​​ode:

[HttpPost]
public JsonResult Upload(HttpPostedFileBase file)
{
    JsonResult FileData = null;
    if (Request != null)
    {
        try
        {
            if (file!=null && file.ContentLength > 0)
            {
                ... do some stuff with the file
            }
        }
        catch (Exception ex)
        {
            Dictionary<string,string> error = new Dictionary<string,string>();
            error.Add("error", "ERROR:" + ex.Message.ToString());
            FileData = Json(error);
        }
    }
    else
    {
        Dictionary<string,string> callResponse = new Dictionary<string,string>();
        callResponse.Add("filename", "You have not specified a file.");
        FileData = Json(callResponse);
    }
    return FileData;
}

任何想法或帮助都将受到赞赏。

谢谢

3 个答案:

答案 0 :(得分:0)

抱歉,我有点困惑,我不知道您是否尝试使用ajax或表单上传文件。如果您使用的是表单,请尝试添加 Interceptor interceptorSecure = chain -> { Request original = chain.request(); // Request customization: add request headers Request.Builder requestBuilder = original.newBuilder() .method(original.method(), original.body()); requestBuilder.header("Authorization", "Bearer " + token); requestBuilder.header("Origin", ORIGIN_URL); Request request = requestBuilder.build(); return chain.proceed(request); }; //after that use this to http client builder //add it to your httpClientBuilder yourHttpClientBuilder.addInterceptor(interceptorSecure); OkHttpClient client = yourHttpClientBuilder.build(); //this is your retrofit builder, the way you have built with base url and gson convertors.. Retrofit retrofit = yourRetrofitBuilder.client(client).build(); retrofit.create(CategoryService.class); 属性。希望这会有所帮助。

答案 1 :(得分:0)

看看这个小提琴链接。 Dojo提供OOTB功能将文件上传到服务器端(您可以传递url,如/test.php

http://jsfiddle.net/kolban/e47YU/

如果您不想传递服务器网址或没有服务器网址,则需要使用dijit.byId("uploader').upload();手动上传文件

答案 2 :(得分:0)

使用<input name="file" ... /> 生成文件输入,但POST方法中的参数名为{{1}} - 它们需要匹配才能使POST方法中的参数绑定到表单的值控制。

更改输入以匹配参数

{{1}}