简体   繁体   中英

Full path of HttpPostedFileBase is giving error with webclient upload file

public ActionResult Index(PublishPost post, HttpPostedFileBase f)
{ 
    string apiUrl = "http://myurl.com"
    WebClient Client = new WebClient();
    byte[] rb = Client.UploadFile(apiUrl, "POST", f.FullName);
    string response = Encoding.UTF8.GetString(rb);
}

The path of httppostefilebase is showing -- C:\\Windows\\SysWOW64\\inetsrv\\adminrights.JPG but it is giving error with webclient - Path not found . Please help

To upload a file using HttpPostedFileBase you can use following snippet:

public void UploadFile(HttpPostedFileBase file)
{
        var folderName = "/Content/Upload/Images/";
        var fileName = file.FileName;
        using (var fileStream = File.Create(BasePath + folderName + fileName))
        {
            file.InputStream.CopyTo(fileStream);
        }
}

Folder where your files are uploaded is not a case as long as IIS has right to write to it.

To delete file just refer to https://msdn.microsoft.com/en-us/library/system.io.file.delete(v=vs.110).aspx

I don't know what you mean by asking whether to delete file after uploading - it depends on your intentions.

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