c# - 无法从动态生成的文件上传控件中保存文件

标签 c# asp.net json asynchronous

我有一个项目,其中文件上传控件是动态生成的。使用 Json,我试图将文件异步保存在目录中。我想在 asp.net 4.0 中使用处理程序模板保存带有文件名的文件。

        $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: Sitepath + "Handler/FileUploader.ashx?filename="+strfiles,
        secureuri: false,
        fileElementClass: "multi",
        dataType: "json",
        async: false
    });

我在 FileUploader 类之外添加了 HTTPPostedFile 的逻辑,因为它会在其中抛出错误。

public class FileUploader : IHttpHandler, IRequiresSessionState

{

public void ProcessRequest(HttpContext context)
{
    string rt = "";


    HttpContext.Current.Response.ContentType = "text/HTML";
    string urlresponse = HttpContext.Current.Request.Url.ToString();
    string tempfiles = "";
    string filenames = "";
    int convert = urlresponse.IndexOf(",");
    urlresponse = urlresponse.Substring(convert + 1);
    string[] filesArray = urlresponse.Split(',');
    for (int i = 0; i < filesArray.Length; i++)
    {
        tempfiles = filesArray[i].ToString();
        int lstIndex=tempfiles.LastIndexOf("\\");
        filenames = filenames + "," + tempfiles.Substring(lstIndex + 1);
    }
    filenames = filenames.Substring(filenames.IndexOf(',') + 1);





    HttpFileCollection uploads = HttpContext.Current.Request.Files;
   string b = HttpContext.Current.Request.Url.ToString();

    Hashtable hashtable = new Hashtable();

    // Declare variable
    string OrderFileName = String.Empty;
    string OrderIDs =String.Empty;
    string TempFolder = String.Empty;

    if (HttpContext.Current.Session["OrderID"] != null)
    {

        OrderIDs = HttpContext.Current.Session["OrderID"].ToString();

        string mapPath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["DocPath"].ToString());

        string mapPathUserId = mapPath + "\\" + HttpContext.Current.Session["UserID"];
      ///
        var g = filenames.Split(',');

        for (int t = 0; t < g.Length;t++ )
        {

           var h = g[t];
           rt = filesArray[t].ToString();

            if (Directory.Exists(mapPathUserId) == false)
            {
                Directory.CreateDirectory(mapPathUserId);
            }
            string mapPathCount = mapPathUserId + "/" + OrderIDs;
            if (Directory.Exists(mapPathCount) == false)
            {
                //--------------Begin(Create Directory)----------------------------------------------------------------------------------//

                Directory.CreateDirectory(mapPathCount);

                //--------------End(Create Directory)----------------------------------------------------------------------------------//

            }
            TempFolder = mapPathUserId + "/" + "Temp";

            if (Directory.Exists(TempFolder) == false)
            {
                //--------------Begin(Create Directory for  temp folder)----------------------------------------------------------------------------------//

                Directory.CreateDirectory(TempFolder);

                //--------------End(Create Directoryfor temp folder)----------------------------------------------------------------------------------//

            }

            OrderFileName = h;
            hashtable.Add(t.ToString(), OrderFileName);

            var see = HttpContext.Current.Server.MapPath(TempFolder + "/" + OrderFileName);
        }

现在,路径创建成功,但文件没有保存到指定目录。我保存文件的代码:

   for (int i = 0; i < uploads.Count; i++)
        {
            HttpPostedFile upload = uploads[i];

            if (Directory.Exists(mapPathUserId) == false)
            {
                Directory.CreateDirectory(mapPathUserId);
            }

            string mapPathCount = mapPathUserId + "/" + OrderIDs;


            if (Directory.Exists(mapPathCount) == false)
            {
                //--------------Begin(Create Directory)----------------------------------------------------------------------------------//

                Directory.CreateDirectory(mapPathCount);

                //--------------End(Create Directory)----------------------------------------------------------------------------------//

            }

            /// Create Path for Temp Folder


            TempFolder = mapPathUserId + "/" + "Temp";

            if (Directory.Exists(TempFolder) == false)
            {
                //--------------Begin(Create Directory for  temp folder)----------------------------------------------------------------------------------//

                Directory.CreateDirectory(TempFolder);

                //--------------End(Create Directoryfor temp folder)----------------------------------------------------------------------------------//

            }

            if (upload.ContentLength > 0)
            {

                if (upload.FileName.Contains(" "))
                {
                    OrderFileName = upload.FileName.Replace(" ", "_");
                }

                else
                {
                    OrderFileName = upload.FileName;

                }

                hashtable.Add(i.ToString(), OrderFileName);
                upload.SaveAs(TempFolder + "/" + OrderFileName);
            }

        }

请帮忙。

最佳答案

您必须确保您通过了正确的路径,在服务器中。我会尝试使用 MapPath() 函数映射路径:

upload.SaveAs(Server.MapPath(TempFolder + "/" + OrderFileName));

关于c# - 无法从动态生成的文件上传控件中保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18941035/

相关文章:

c# - 在 WCF UsernamePasswordValidator 中访问当前的 InstanceContext

c# - .Net 4.0 框架和 4.5 框架之间的区别?

android - Android App连接SQL Server数据库的方法

javascript - 使用 javascript 连接两个数组

php - 我在 zend framework1 上提交表单时如何传递消息

javascript - 在模式窗口 MVC4 上显示下拉列表

c# - 关系之间的内部连接

c# - XML 排序异常

asp.net - 输出缓存 : VaryByHeader ="User-Agent" or VaryByCustom ="Browser"?

javascript - 使用 i18next 构建菜单