goahead文件下载

1. goahead下载文件源码

static char   *websIndex;                   /* 默认页面名称 */
static char   *websDocuments;               /* 默认页面目录 */

static void closeFile()
{
    wfree(websIndex); 
    websIndex = NULL;
    wfree(websDocuments);
    websDocuments = NULL;
}
static void fileWriteEvent(Webs *wp)
{

    char    *buf;
    ssize   len, wrote;

    assert(wp);
    assert(websValid(wp));


    if ((buf = walloc(ME_GOAHEAD_LIMIT_BUFFER)) == NULL) {
        websError(wp, HTTP_CODE_INTERNAL_SERVER_ERROR, "Can't get memory");
        return;
    }
     while ((len = websPageReadData(wp, buf, ME_GOAHEAD_LIMIT_BUFFER)) > 0) {
        if ((wrote = websWriteSocket(wp, buf, len)) < 0) {
            break;
        }
        if (wrote != len) {
            websPageSeek(wp, - (len - wrote), SEEK_CUR);
            break;
        }
    }
    wfree(buf);
    if (len <= 0) {
        websDone(wp);
    }
    }

static bool fileHandler(Webs *wp)
{
    WebsFileInfo    info;
    char            *tmp, *date;
    ssize           nchars;
    int             code;

    char    *pathfilename; //带路径的文件名 用于找到对应的文件  
    char    *filenameExt; //文件扩展名 用于 设置 MIME类型  
    char    *filename;      //文件名 用于下载后保存的文件名称  
    char    *disposition; //临时保存 附件 标识  

    assert(websValid(wp));
    assert(wp->method);
    assert(wp->filename && wp->filename[0]);    

    pathfilename = websGetVar(wp, "filePath", NULL);
    if (pathfilename==NULL)
        return 1;

    //取文件名和扩展名
    filename =sclone(getUrlLastSplit(sclone(pathfilename),"/"));
    filenameExt =sclone(getUrlLastSplit(sclone(filename),"."));

    if (wp->ext) wfree(wp->ext);

    wp->ext=walloc(1+strlen(filenameExt)+1);
    sprintf(wp->ext,".%s",sclone(filenameExt));
    free(filenameExt);
    filenameExt=NULL;

    if (wp->filename) wfree(wp->filename);
    wp->filename=sclone(pathfilename);

    if (wp->path) wfree(wp->path);

    wp->path=sclone(pathfilename);
    #if !BIT_ROM
    if (smatch(wp->method, "DELETE")) {
        if (unlink(wp->filename) < 0) {
            websError(wp, HTTP_CODE_NOT_FOUND, "Can't delete the URI");
        } else {
            /* No content */
            websResponse(wp, 204, 0);
        }
    } else if (smatch(wp->method, "PUT")) {

        websResponse(wp, wp->code, 0);

    } else 
    #endif /* !BIT_ROM */
    {


        if (websPageIsDirectory(wp)) {
            nchars = strlen(wp->path);
            if (wp->path[nchars - 1] == '/' || wp->path[nchars - 1] == '\\') {
                wp->path[--nchars] = '\0';
            }
            tmp = sfmt("%s/%s", wp->path, websIndex);
            websRedirect(wp, tmp);
            wfree(tmp);
            return 1;
        }

        if (websPageOpen(wp, O_RDONLY | O_BINARY, 0666) < 0) {
#if BIT_DEBUG
            if (wp->referrer) {
                trace(1, "From %s", wp->referrer);
            }
  #endif
     websError(wp, HTTP_CODE_NOT_FOUND, "Cannot open   document for: %s", wp->path);
            return 1;
        }   
         if (websPageStat(wp, &info) < 0) {
            websError(wp, HTTP_CODE_NOT_FOUND, "Cannot stat page for URL");
            return 1;
        }

        code = 200;
        if (wp->since && info.mtime <= wp->since) {
            code = 304;
        }

        websSetStatus(wp, code);
        websWriteHeaders(wp, info.size, 0);
        disposition = walloc(20+strlen(filename)+1);
    //设置下载文件的名称
    sprintf(disposition,"attachment;filename=%s",sclone(filename));
        websWriteHeader(wp, "Content-Disposition", sclone(disposition));

        free(filename);
        free(disposition);
        filename=NULL;
        disposition=NULL;  
         if ((date = websGetDateString(&info)) != NULL) {
            websWriteHeader(wp, "Last-modified", "%s", date);
            wfree(date);
        }

        websWriteEndHeaders(wp);

        /*
            All done if the browser did a HEAD request
         */
        if (smatch(wp->method, "HEAD")) {
            websDone(wp);
            return 1;
        }
        websSetBackgroundWriter(wp, fileWriteEvent);

    }

    return 1;
}
/***
  *goahead中的Action函数,前台可通过/action/downLoadFileAction访问
  */
static void downLoadFileAction(Webs *wp, char *path,char*query)
 { 
 //保存原来的回调函数 
     WebsHandlerProc service = (*wp).route->handler>service; 
    //设置文件下载的handler函数
    (*wp).route->handler->service =(*fileHandler); 
    (*wp).route->handler->service(wp); 
    //恢复原来的回调函数
    (*wp).route->handler->service= service; 
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值