简体   繁体   中英

Using MVC Controller to replace .ashx

I need a controller to handle file uploads. Is it possible to just have a handler print text directly to the page rather than return view(); ?

public ActionResult Upload(HttpContext context)
        {
            HttpPostedFile file = context.Request.Files["fileData"];

            Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;
            string userID = userGuid.ToString();

            string targetLocation = "D:\\inetpub\\wwwroot\\RTDOTNETMEMBER\\audio\\songs\\mp3\\" + userID + "\\" + file.FileName;

            file.SaveAs(targetLocation);

            Response.Write("Testing");
}

Perhaps using ContentResult will do the job:

return new ContentResult() { 
    Content = "Testing", 
    ContentEncoding = System.Text.Encoding.UTF32, 
    ContentType = "text/plain" 
};

Just change the return type of your action method to string and return a string. It would look somethig like this:

  public string ReturnString() { return "Just a string"; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM