如何使用multipart / formdata在Restangularjs中上传文件

时间:2015-11-16 07:43:04

标签: angularjs angular-file-upload

我的HTML代码

    public Dictionary<string, Type> ControlTypes
    {
        get { return (Dictionary<string, Type>)Session["ControlTypes"]; }
        set { Session["ControlTypes"] = value; }
    }
    public int NumberOfControls
    {
        get { return (int)ViewState["NumOfControls"]; }
        set { ViewState["NumOfControls"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.NumberOfControls = 0;
            ControlTypes = new Dictionary<string, Type>();
        }
        else
            this.CreateControls();
    }

    private void CreateControls()
    {
        for (int counter = 0; counter < this.NumberOfControls; counter++)
        {
            string controlId = "control_id_" + counter.ToString();
            Type controlType = ControlTypes[controlId];
            ProperyInfo[] properties = controlType.GetProperties();
            Object controlObject = Activator.CreateInstance(controlType);
            foreach (var propery in properties)
            {
                If (propery.Name == "ID")
                    Property.SetValue(controlObject, controlId, null);
            }
            panel.Controls.Add(controlObject as Control)
        }
    }

    protected void ddlControlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (ddlControlType.SelectedIndex)
        {
            case 0:
            case 2:
                txtboxText.Enabled = true;
                txtboxGroupName.Enabled = false;
                break;
            case 1:
                txtboxText.Enabled = false;
                txtboxGroupName.Enabled = false;
                break;
            case 3:
                txtboxText.Enabled = true;
                txtboxGroupName.Enabled = true;
                break;
        }
    }

    protected void btnAddControl_Click(object sender, EventArgs e)
    {
        Control tempControl = null;
        switch (ddlControlType.SelectedIndex)
        {
            case 0:
                tempControl = new Label();
                ((Label)tempControl).Text = txtboxText.Text;
                break;
            case 1:
                tempControl = new TextBox();
                break;
            case 2:
                tempControl = new CheckBox();
                ((CheckBox)tempControl).Text = txtboxText.Text;
                break;
            case 3:
                tempControl = new RadioButton();
                ((RadioButton)tempControl).Text = txtboxText.Text;
                ((RadioButton)tempControl).GroupName = txtboxGroupName.Text;
                break;
        }
        string controlId = "control_id_" + this.NumberOfControls.ToString();
        tempControl.ID = controlId;
        panel.Controls.Add(tempControl);
        this.NumberOfControls++;
        ControlTypes.Add(controlId, tempControl.GetType());
    }

感谢您的帮助

1 个答案:

答案 0 :(得分:4)

这段代码对我有用......

$scope.uploadFiles = function(file) {
                console.log(file);
                $scope.fileData = file;
                var fd = new FormData();
                fd.append('file', file);
                Restangular.one('/api/files/end points').withHttpConfig({transformRequest: angular.identity})
                    .customPOST(fd, '', undefined, {'Content-Type': undefined})
            };