C# 保存文件时MVC Dropzone.js回发

C# 保存文件时MVC Dropzone.js回发,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我正在尝试使用Dropzone.js上传文件。我的代码使用ajax调用我的控制器,但在保存文件后会进行回发。我怎么能在没有邮件的情况下做到这一点 我的代码: HTML @**@ JAVASCRIPT Dropzone.options.dzCampaingImages = { //prevents Dropzone from uploading dropped files immediately autoProcessQueue: false,

我正在尝试使用Dropzone.js上传文件。我的代码使用ajax调用我的控制器,但在保存文件后会进行回发。我怎么能在没有邮件的情况下做到这一点

我的代码:

HTML


@**@
JAVASCRIPT

Dropzone.options.dzCampaingImages = {

            //prevents Dropzone from uploading dropped files immediately
            autoProcessQueue: false,
            uploadMultiple: true,
            init: function () {
                var submitButton = document.querySelector("#btnUploadSubmit");
                var myDropzone = this; //closure

                submitButton.addEventListener("click", function (files) {

                    //tell Dropzone to process all queued files
                    //myDropzone.processQueue();

                    //return false;
                    //var data = new FormData();
                    //for (var i = 0; i < files.length; i++) {
                    //    data.append(files[i].name, files[i]);
                    //};

                    var data = new FormData();
                    for (var i = 0; i < myDropzone.files.length; i++) {
                        data.append(myDropzone.files[i].name, myDropzone.files[i]);
                    };

                    $.ajax({
                        url: '@Url.Content("~/Promotion/UploadImages")',
                        type: "POST",
                        processData: false,
                        contentType: false,
                        data: data,
                        success: function (response) {
                            //code after success
                            return false;
                        },
                        error: function (er) {
                            alert(er);
                        }

                    });
                });
Dropzone.options.dzCampaingImages={
//防止Dropzone立即上载删除的文件
自动处理队列:false,
uploadMultiple:true,
init:函数(){
var submitButton=document.querySelector(“#btnUploadSubmit”);
var myDropzone=this;//闭包
submitButton.addEventListener(“单击”),函数(文件){
//告诉Dropzone处理所有排队的文件
//myDropzone.processQueue();
//返回false;
//var data=new FormData();
//对于(var i=0;i
C#

public void UploadImages()
{
bool isUpload=true;
HttpContext=System.Web.HttpContext.Current;
尝试
{
//foreach(Request.Files中的字符串文件名)
//{
//HttpPostedFileBase file=Request.Files[fileName];
////您可以在此处保存文件内容
//    //
//字符串servPath=ConfigurationManager.AppSettings[“PathTemp_CampaingImages”];
//字符串servFilePath=Request.RequestContext.HttpContext.Server.MapPath(servPath+file.FileName);
//    // 
////使用(FileStream outStream=new FileStream(servFilePath,FileMode.CreateNew,FileAccess.Write))
//    //{
//    //    // 
/////file.InputStream.CopyTo(扩展);
//    //}
//    // 
//SaveAs(servFilePath);
//}
字符串servPath=context.Server.MapPath(ConfigurationManager.AppSettings[“pathttemp\u CampaingImages]”);
//字符串servFilePath=Request.RequestContext.HttpContext.Server.MapPath(servPath+file.FileName);
字符串filePath=servPath;
//在这里编写处理程序实现。
如果(context.Request.Files.Count,这是因为默认情况下,“提交”按钮也会使用传统的完整页面提交来提交表单,而不管单击时是否运行JavaScript

你也可以


1) 将提交按钮变成
问题在于,由于浏览器链接功能,Visual Studio正在向我的代码中注入javascript。我注意到这是因为浏览器开发人员控制台中出现了一个错误

这是帮助我的链接

另一个有用的链接,

Dropzone.options.dzCampaingImages = {

            //prevents Dropzone from uploading dropped files immediately
            autoProcessQueue: false,
            uploadMultiple: true,
            init: function () {
                var submitButton = document.querySelector("#btnUploadSubmit");
                var myDropzone = this; //closure

                submitButton.addEventListener("click", function (files) {

                    //tell Dropzone to process all queued files
                    //myDropzone.processQueue();

                    //return false;
                    //var data = new FormData();
                    //for (var i = 0; i < files.length; i++) {
                    //    data.append(files[i].name, files[i]);
                    //};

                    var data = new FormData();
                    for (var i = 0; i < myDropzone.files.length; i++) {
                        data.append(myDropzone.files[i].name, myDropzone.files[i]);
                    };

                    $.ajax({
                        url: '@Url.Content("~/Promotion/UploadImages")',
                        type: "POST",
                        processData: false,
                        contentType: false,
                        data: data,
                        success: function (response) {
                            //code after success
                            return false;
                        },
                        error: function (er) {
                            alert(er);
                        }

                    });
                });
public void UploadImages()
    {
        bool isUpload = true;

        HttpContext context = System.Web.HttpContext.Current;

        try
        {
            //foreach (string fileName in Request.Files)
            //{
            //    HttpPostedFileBase file = Request.Files[fileName];

            //    //You can Save the file content here
            //    //
            //    string servPath = ConfigurationManager.AppSettings["PathTemp_CampaingImages"];
            //    string servFilePath = Request.RequestContext.HttpContext.Server.MapPath(servPath + file.FileName);

            //    // 
            //    //using (FileStream outStream = new FileStream(servFilePath, FileMode.CreateNew, FileAccess.Write))
            //    //{
            //    //    // 
            //    //    //file.InputStream.CopyTo(outStream);

            //    //}

            //    // 
            //    file.SaveAs(servFilePath);
            //}

            string servPath = context.Server.MapPath(ConfigurationManager.AppSettings["PathTemp_CampaingImages"]);
            //string servFilePath = Request.RequestContext.HttpContext.Server.MapPath(servPath + file.FileName);

            string filePath = servPath;

            //write your handler implementation here.
            if (context.Request.Files.Count <= 0)
            {
                context.Response.Write("No file uploaded");
            }
            else
            {
                for (int i = 0; i < context.Request.Files.Count; ++i)
                {
                    HttpPostedFile file = context.Request.Files[i];
                    if (context.Request.Form != null)
                    {
                        string imageid = context.Request.Form.ToString();
                        imageid = imageid.Substring(imageid.IndexOf('=') + 1);

                        if (file != null)
                        {
                            string ext = file.FileName.Substring(file.FileName.IndexOf('.'));
                            if (ext.ToLower().Contains("gif") || ext.ToLower().Contains("jpg") || ext.ToLower().Contains("jpeg") || ext.ToLower().Contains("png"))
                            {

                                byte[] data;
                                using (Stream inputStream = file.InputStream)
                                {
                                    MemoryStream memoryStream = inputStream as MemoryStream;
                                    if (memoryStream == null)
                                    {
                                        memoryStream = new MemoryStream();
                                        inputStream.CopyTo(memoryStream);
                                    }
                                    data = memoryStream.ToArray();
                                    //System.IO.File.WriteAllBytes(filePath + file.FileName + ".jpg", (byte[])data);
                                    //club.club_image = Convert.ToBase64String(data);
                                }
                            }
                        }
                    }
                    else
                    {

                    }

                    //file.SaveAs(context.Server.MapPath(filePath + file.FileName));
                    //context.Response.Write("File uploaded");
                }
            }
        }
submitButton.addEventListener("click", function (event) {
  event.preventDefault();
  //...etc